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 #ifdef NONAMELESSUNION
36 #ifdef NONAMELESSSTRUCT
42 static int dll_capable(const char *dll
, const char *function
)
44 HMODULE module
= GetModuleHandleA(dll
);
45 if (!module
) return 0;
47 return (GetProcAddress(module
, function
) != NULL
);
51 LPCSTR filename
= "testfile.xxx";
53 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
55 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
56 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
57 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
58 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
59 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
60 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
61 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
62 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
65 static void test__hread( void )
74 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
75 DeleteFileA( filename
);
76 filehandle
= _lcreat( filename
, 0 );
77 if (filehandle
== HFILE_ERROR
)
79 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
83 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
85 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
87 filehandle
= _lopen( filename
, OF_READ
);
89 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%ld)\n", filename
, GetLastError( ) );
91 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
93 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
95 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
97 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
98 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
99 for (i
= 0; i
< bytes_wanted
; i
++)
101 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
105 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
107 ret
= DeleteFileA( filename
);
108 ok( ret
!= 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
112 static void test__hwrite( void )
121 HLOCAL memory_object
;
125 filehandle
= _lcreat( filename
, 0 );
126 if (filehandle
== HFILE_ERROR
)
128 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
132 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
134 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
136 filehandle
= _lopen( filename
, OF_READ
);
138 bytes_read
= _hread( filehandle
, buffer
, 1);
140 ok( 0 == bytes_read
, "file read size error\n" );
142 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
144 filehandle
= _lopen( filename
, OF_READWRITE
);
148 srand( (unsigned)time( NULL
) );
149 for (blocks
= 0; blocks
< 100; blocks
++)
151 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
154 checksum
[0] = checksum
[0] + buffer
[i
];
156 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
157 bytes_written
= bytes_written
+ sizeof( buffer
);
160 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
163 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
165 memory_object
= LocalAlloc( LPTR
, bytes_written
);
167 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
169 contents
= LocalLock( memory_object
);
171 filehandle
= _lopen( filename
, OF_READ
);
173 contents
= LocalLock( memory_object
);
175 ok( NULL
!= contents
, "LocalLock whines\n" );
177 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
183 checksum
[0] = checksum
[0] + contents
[i
];
186 while (i
< bytes_written
- 1);
188 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
190 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
192 ret
= DeleteFileA( filename
);
193 ok( ret
!= 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
197 static void test__lclose( void )
202 filehandle
= _lcreat( filename
, 0 );
203 if (filehandle
== HFILE_ERROR
)
205 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
209 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
211 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
213 ret
= DeleteFileA( filename
);
214 ok( ret
!= 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
218 static void test__lcreat( void )
222 WIN32_FIND_DATAA search_results
;
223 char slashname
[] = "testfi/";
228 filehandle
= _lcreat( filename
, 0 );
229 if (filehandle
== HFILE_ERROR
)
231 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
235 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
237 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
239 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
241 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
243 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
245 ret
= DeleteFileA(filename
);
246 ok( ret
!= 0, "DeleteFile failed (%ld)\n", GetLastError());
248 filehandle
= _lcreat( filename
, 1 ); /* readonly */
249 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%ld)\n", filename
, GetLastError( ) );
251 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
253 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
255 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
257 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
259 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
261 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
263 filehandle
= _lcreat( filename
, 2 );
264 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%ld)\n", filename
, GetLastError( ) );
266 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
268 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
270 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
272 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
274 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
276 ret
= DeleteFileA( filename
);
277 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
279 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
280 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%ld)\n", filename
, GetLastError( ) );
282 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
284 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
286 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
288 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
290 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
292 ret
= DeleteFileA( filename
);
293 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
295 filehandle
=_lcreat (slashname
, 0); /* illegal name */
296 if (HFILE_ERROR
==filehandle
) {
298 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
299 "creating file \"%s\" failed with error %d\n", slashname
, err
);
300 } else { /* only NT succeeds */
302 find
=FindFirstFileA (slashname
, &search_results
);
303 if (INVALID_HANDLE_VALUE
!=find
)
305 ret
= FindClose (find
);
306 ok (0 != ret
, "FindClose complains (%ld)\n", GetLastError ());
307 slashname
[strlen(slashname
)-1]=0;
308 ok (!strcmp (slashname
, search_results
.cFileName
),
309 "found unexpected name \"%s\"\n", search_results
.cFileName
);
310 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
311 "attributes of file \"%s\" are 0x%04lx\n", search_results
.cFileName
,
312 search_results
.dwFileAttributes
);
314 ret
= DeleteFileA( slashname
);
315 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
318 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
319 if (HFILE_ERROR
==filehandle
)
320 ok (0, "couldn't create volume label \"%s\"\n", filename
);
323 find
=FindFirstFileA (filename
, &search_results
);
324 if (INVALID_HANDLE_VALUE
==find
)
325 ok (0, "file \"%s\" not found\n", filename
);
327 ret
= FindClose(find
);
328 ok ( 0 != ret
, "FindClose complains (%ld)\n", GetLastError ());
329 ok (!strcmp (filename
, search_results
.cFileName
),
330 "found unexpected name \"%s\"\n", search_results
.cFileName
);
331 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
332 "attributes of file \"%s\" are 0x%04lx\n", search_results
.cFileName
,
333 search_results
.dwFileAttributes
);
335 ret
= DeleteFileA( filename
);
336 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
341 static void test__llseek( void )
349 filehandle
= _lcreat( filename
, 0 );
350 if (filehandle
== HFILE_ERROR
)
352 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
356 for (i
= 0; i
< 400; i
++)
358 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
360 ok( HFILE_ERROR
!= _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
), "should be able to seek\n" );
361 ok( HFILE_ERROR
!= _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
), "should be able to seek\n" );
363 bytes_read
= _hread( filehandle
, buffer
, 1);
364 ok( 1 == bytes_read
, "file read size error\n" );
365 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
366 ok( HFILE_ERROR
!= _llseek( filehandle
, -400 * strlen( sillytext
), FILE_END
), "should be able to seek\n" );
368 bytes_read
= _hread( filehandle
, buffer
, 1);
369 ok( 1 == bytes_read
, "file read size error\n" );
370 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
371 ok( HFILE_ERROR
!= _llseek( filehandle
, 1000000, FILE_END
), "should be able to seek past file; poor, poor Windows programmers\n" );
372 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
374 ret
= DeleteFileA( filename
);
375 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
379 static void test__llopen( void )
386 filehandle
= _lcreat( filename
, 0 );
387 if (filehandle
== HFILE_ERROR
)
389 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
393 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
394 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
396 filehandle
= _lopen( filename
, OF_READ
);
397 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
398 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
399 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
400 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
402 filehandle
= _lopen( filename
, OF_READWRITE
);
403 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
404 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
405 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
406 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
408 filehandle
= _lopen( filename
, OF_WRITE
);
409 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
410 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
411 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
413 ret
= DeleteFileA( filename
);
414 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
415 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
419 static void test__lread( void )
428 filehandle
= _lcreat( filename
, 0 );
429 if (filehandle
== HFILE_ERROR
)
431 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
435 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
437 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
439 filehandle
= _lopen( filename
, OF_READ
);
441 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%ld)\n", filename
, GetLastError());
443 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
445 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
447 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
449 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
450 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
451 for (i
= 0; i
< bytes_wanted
; i
++)
453 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
457 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
459 ret
= DeleteFileA( filename
);
460 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
464 static void test__lwrite( void )
473 HLOCAL memory_object
;
477 filehandle
= _lcreat( filename
, 0 );
478 if (filehandle
== HFILE_ERROR
)
480 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
484 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
486 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
488 filehandle
= _lopen( filename
, OF_READ
);
490 bytes_read
= _hread( filehandle
, buffer
, 1);
492 ok( 0 == bytes_read
, "file read size error\n" );
494 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
496 filehandle
= _lopen( filename
, OF_READWRITE
);
500 srand( (unsigned)time( NULL
) );
501 for (blocks
= 0; blocks
< 100; blocks
++)
503 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
506 checksum
[0] = checksum
[0] + buffer
[i
];
508 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
509 bytes_written
= bytes_written
+ sizeof( buffer
);
512 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
515 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
517 memory_object
= LocalAlloc( LPTR
, bytes_written
);
519 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
521 contents
= LocalLock( memory_object
);
523 filehandle
= _lopen( filename
, OF_READ
);
525 contents
= LocalLock( memory_object
);
527 ok( NULL
!= contents
, "LocalLock whines\n" );
529 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
535 checksum
[0] += contents
[i
];
538 while (i
< bytes_written
- 1);
540 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
542 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
544 ret
= DeleteFileA( filename
);
545 ok( ret
, "DeleteFile failed (%ld)\n", GetLastError( ) );
548 static void test_CopyFileA(void)
550 char temp_path
[MAX_PATH
];
551 char source
[MAX_PATH
], dest
[MAX_PATH
];
552 static const char prefix
[] = "pfx";
559 ret
= GetTempPathA(MAX_PATH
, temp_path
);
560 ok(ret
!= 0, "GetTempPathA error %ld\n", GetLastError());
561 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
563 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
564 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
566 /* make the source have not zero size */
567 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
568 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
569 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
570 ok( retok
&& ret
== sizeof(prefix
),
571 "WriteFile error %ld\n", GetLastError());
572 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
573 /* get the file time and change it to prove the difference */
574 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
575 ok( ret
, "GetFileTime error %ld\n", GetLastError());
576 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
577 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
578 ok( ret
, "SetFileTime error %ld\n", GetLastError());
579 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
582 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
583 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
585 SetLastError(0xdeadbeef);
586 ret
= CopyFileA(source
, dest
, TRUE
);
587 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
588 "CopyFileA: unexpected error %ld\n", GetLastError());
590 ret
= CopyFileA(source
, dest
, FALSE
);
591 ok(ret
, "CopyFileA: error %ld\n", GetLastError());
593 /* make sure that destination has correct size */
594 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
595 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
596 ret
= GetFileSize(hfile
, NULL
);
597 ok(ret
== sizeof(prefix
), "destination file has wrong size %ld\n", ret
);
599 /* make sure that destination has the same filetime */
600 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
601 ok( ret
, "GetFileTime error %ld\n", GetLastError());
602 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
604 SetLastError(0xdeadbeef);
605 ret
= CopyFileA(source
, dest
, FALSE
);
606 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
607 "CopyFileA: ret = %ld, unexpected error %ld\n", ret
, GetLastError());
609 /* make sure that destination still has correct size */
610 ret
= GetFileSize(hfile
, NULL
);
611 ok(ret
== sizeof(prefix
), "destination file has wrong size %ld\n", ret
);
612 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
613 ok( retok
&& ret
== sizeof(prefix
),
614 "ReadFile: error %ld\n", GetLastError());
615 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
618 ret
= DeleteFileA(source
);
619 ok(ret
, "DeleteFileA: error %ld\n", GetLastError());
620 ret
= DeleteFileA(dest
);
621 ok(ret
, "DeleteFileA: error %ld\n", GetLastError());
624 static void test_CopyFileW(void)
626 WCHAR temp_path
[MAX_PATH
];
627 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
628 static const WCHAR prefix
[] = {'p','f','x',0};
631 ret
= GetTempPathW(MAX_PATH
, temp_path
);
632 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
634 ok(ret
!= 0, "GetTempPathW error %ld\n", GetLastError());
635 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
637 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
638 ok(ret
!= 0, "GetTempFileNameW error %ld\n", GetLastError());
640 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
641 ok(ret
!= 0, "GetTempFileNameW error %ld\n", GetLastError());
643 ret
= CopyFileW(source
, dest
, TRUE
);
644 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
645 "CopyFileW: unexpected error %ld\n", GetLastError());
647 ret
= CopyFileW(source
, dest
, FALSE
);
648 ok(ret
, "CopyFileW: error %ld\n", GetLastError());
650 ret
= DeleteFileW(source
);
651 ok(ret
, "DeleteFileW: error %ld\n", GetLastError());
652 ret
= DeleteFileW(dest
);
653 ok(ret
, "DeleteFileW: error %ld\n", GetLastError());
656 static void test_CreateFileA(void)
659 char temp_path
[MAX_PATH
];
660 char filename
[MAX_PATH
];
661 static const char prefix
[] = "pfx";
664 ret
= GetTempPathA(MAX_PATH
, temp_path
);
665 ok(ret
!= 0, "GetTempPathA error %ld\n", GetLastError());
666 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
668 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
669 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
671 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
672 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
673 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
674 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
676 ret
= DeleteFileA(filename
);
677 ok(ret
, "DeleteFileA: error %ld\n", GetLastError());
680 static void test_CreateFileW(void)
683 WCHAR temp_path
[MAX_PATH
];
684 WCHAR filename
[MAX_PATH
];
685 static const WCHAR emptyW
[]={'\0'};
686 static const WCHAR prefix
[] = {'p','f','x',0};
687 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
690 ret
= GetTempPathW(MAX_PATH
, temp_path
);
691 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
693 ok(ret
!= 0, "GetTempPathW error %ld\n", GetLastError());
694 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
696 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
697 ok(ret
!= 0, "GetTempFileNameW error %ld\n", GetLastError());
699 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
700 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
701 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
702 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
704 ret
= DeleteFileW(filename
);
705 ok(ret
, "DeleteFileW: error %ld\n", GetLastError());
707 #if 0 /* this test crashes on NT4.0 */
708 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
709 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
710 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
711 "CreateFileW(NULL) returned ret=%p error=%ld\n",hFile
,GetLastError());
714 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
715 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
716 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
717 "CreateFileW(\"\") returned ret=%p error=%ld\n",hFile
,GetLastError());
719 /* test the result of opening a nonexistent driver name */
720 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
721 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
722 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
723 "CreateFileW on invalid VxD name returned ret=%p error=%ld\n",hFile
,GetLastError());
726 static void test_GetTempFileNameA()
730 char expected
[MAX_PATH
+ 10];
731 char windowsdir
[MAX_PATH
+ 10];
732 char windowsdrive
[3];
734 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
735 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
736 ok(result
!= 0, "GetWindowsDirectory: error %ld\n", GetLastError());
738 /* If the Windows directory is the root directory, it ends in backslash, not else. */
739 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
741 strcat(windowsdir
, "\\");
744 windowsdrive
[0] = windowsdir
[0];
745 windowsdrive
[1] = windowsdir
[1];
746 windowsdrive
[2] = '\0';
748 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
749 ok(result
!= 0, "GetTempFileNameA: error %ld\n", GetLastError());
750 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
751 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
752 windowsdrive
[0], out
);
754 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
755 ok(result
!= 0, "GetTempFileNameA: error %ld\n", GetLastError());
757 strcat(expected
, windowsdir
);
758 strcat(expected
, "abc2.tmp");
759 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
763 static void test_DeleteFileA( void )
767 ret
= DeleteFileA(NULL
);
768 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
769 GetLastError() == ERROR_PATH_NOT_FOUND
),
770 "DeleteFileA(NULL) returned ret=%d error=%ld\n",ret
,GetLastError());
772 ret
= DeleteFileA("");
773 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
774 GetLastError() == ERROR_BAD_PATHNAME
),
775 "DeleteFileA(\"\") returned ret=%d error=%ld\n",ret
,GetLastError());
777 ret
= DeleteFileA("nul");
778 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
779 GetLastError() == ERROR_INVALID_PARAMETER
||
780 GetLastError() == ERROR_ACCESS_DENIED
),
781 "DeleteFileA(\"nul\") returned ret=%d error=%ld\n",ret
,GetLastError());
784 static void test_DeleteFileW( void )
787 static const WCHAR emptyW
[]={'\0'};
789 ret
= DeleteFileW(NULL
);
790 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
792 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
793 "DeleteFileW(NULL) returned ret=%d error=%ld\n",ret
,GetLastError());
795 ret
= DeleteFileW(emptyW
);
796 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
797 "DeleteFileW(\"\") returned ret=%d error=%ld\n",ret
,GetLastError());
800 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
802 static void test_MoveFileA(void)
804 char tempdir
[MAX_PATH
];
805 char source
[MAX_PATH
], dest
[MAX_PATH
];
806 static const char prefix
[] = "pfx";
809 ret
= GetTempPathA(MAX_PATH
, tempdir
);
810 ok(ret
!= 0, "GetTempPathA error %ld\n", GetLastError());
811 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
813 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
814 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
816 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
817 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
819 ret
= MoveFileA(source
, dest
);
820 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
821 "MoveFileA: unexpected error %ld\n", GetLastError());
823 ret
= DeleteFileA(dest
);
824 ok(ret
, "DeleteFileA: error %ld\n", GetLastError());
826 ret
= MoveFileA(source
, dest
);
827 ok(ret
, "MoveFileA: failed, error %ld\n", GetLastError());
829 lstrcatA(tempdir
, "Remove Me");
830 ret
= CreateDirectoryA(tempdir
, NULL
);
831 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
833 lstrcpyA(source
, dest
);
834 lstrcpyA(dest
, tempdir
);
835 lstrcatA(dest
, "\\wild?.*");
836 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
837 ret
= MoveFileA(source
, dest
);
838 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
839 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
840 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
841 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
842 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
845 char temppath
[MAX_PATH
];
848 lstrcpyA(temppath
, tempdir
);
849 lstrcatA(temppath
, "\\*.*");
850 hFind
= FindFirstFileA(temppath
, &fd
);
851 if (INVALID_HANDLE_VALUE
!= hFind
)
856 lpName
= fd
.cAlternateFileName
;
858 lpName
= fd
.cFileName
;
859 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
861 while (FindNextFileA(hFind
, &fd
));
865 ret
= DeleteFileA(source
);
866 ok(ret
, "DeleteFileA: error %ld\n", GetLastError());
867 ret
= DeleteFileA(dest
);
868 ok(!ret
, "DeleteFileA: error %ld\n", GetLastError());
869 ret
= RemoveDirectoryA(tempdir
);
870 ok(ret
, "DeleteDirectoryA: error %ld\n", GetLastError());
873 static void test_MoveFileW(void)
875 WCHAR temp_path
[MAX_PATH
];
876 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
877 static const WCHAR prefix
[] = {'p','f','x',0};
880 ret
= GetTempPathW(MAX_PATH
, temp_path
);
881 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
883 ok(ret
!= 0, "GetTempPathW error %ld\n", GetLastError());
884 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
886 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
887 ok(ret
!= 0, "GetTempFileNameW error %ld\n", GetLastError());
889 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
890 ok(ret
!= 0, "GetTempFileNameW error %ld\n", GetLastError());
892 ret
= MoveFileW(source
, dest
);
893 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
894 "CopyFileW: unexpected error %ld\n", GetLastError());
896 ret
= DeleteFileW(source
);
897 ok(ret
, "DeleteFileW: error %ld\n", GetLastError());
898 ret
= DeleteFileW(dest
);
899 ok(ret
, "DeleteFileW: error %ld\n", GetLastError());
902 #define PATTERN_OFFSET 0x10
904 static void test_offset_in_overlapped_structure(void)
910 BYTE buf
[256], pattern
[] = "TeSt";
912 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
915 ret
=GetTempPathA(MAX_PATH
, temp_path
);
916 ok( ret
, "GetTempPathA error %ld\n", GetLastError());
917 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
918 ok( ret
, "GetTempFileNameA error %ld\n", GetLastError());
920 /*** Write File *****************************************************/
922 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
923 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %ld\n", GetLastError());
925 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
926 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
927 ok( ret
, "WriteFile error %ld\n", GetLastError());
928 ok(done
== sizeof(buf
), "expected number of bytes written %lu\n", done
);
930 memset(&ov
, 0, sizeof(ov
));
931 S(U(ov
)).Offset
= PATTERN_OFFSET
;
932 S(U(ov
)).OffsetHigh
= 0;
933 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
934 /* Win 9x does not support the overlapped I/O on files */
935 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
936 ok(rc
, "WriteFile error %ld\n", GetLastError());
937 ok(done
== sizeof(pattern
), "expected number of bytes written %lu\n", done
);
938 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
939 ok(SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
) == (PATTERN_OFFSET
+ sizeof(pattern
)),
940 "expected file offset %d\n", PATTERN_OFFSET
+ sizeof(pattern
));
942 S(U(ov
)).Offset
= sizeof(buf
) * 2;
943 S(U(ov
)).OffsetHigh
= 0;
944 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
945 ok( ret
, "WriteFile error %ld\n", GetLastError());
946 ok(done
== sizeof(pattern
), "expected number of bytes written %lu\n", done
);
947 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
948 ok(SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
) == (sizeof(buf
) * 2 + sizeof(pattern
)),
949 "expected file offset %d\n", sizeof(buf
) * 2 + sizeof(pattern
));
954 /*** Read File *****************************************************/
956 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
957 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %ld\n", GetLastError());
959 memset(buf
, 0, sizeof(buf
));
960 memset(&ov
, 0, sizeof(ov
));
961 S(U(ov
)).Offset
= PATTERN_OFFSET
;
962 S(U(ov
)).OffsetHigh
= 0;
963 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
964 /* Win 9x does not support the overlapped I/O on files */
965 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
966 ok(rc
, "ReadFile error %ld\n", GetLastError());
967 ok(done
== sizeof(pattern
), "expected number of bytes read %lu\n", done
);
968 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
969 ok(SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
) == (PATTERN_OFFSET
+ sizeof(pattern
)),
970 "expected file offset %d\n", PATTERN_OFFSET
+ sizeof(pattern
));
971 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
976 ret
= DeleteFileA(temp_fname
);
977 ok( ret
, "DeleteFileA error %ld\n", GetLastError());
980 static void test_LockFile(void)
984 OVERLAPPED overlapped
;
985 int limited_LockFile
;
986 int limited_UnLockFile
;
987 int lockfileex_capable
;
989 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
990 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
991 CREATE_ALWAYS
, 0, 0 );
992 if (handle
== INVALID_HANDLE_VALUE
)
994 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
997 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
999 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1000 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1002 limited_UnLockFile
= 0;
1003 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1005 limited_UnLockFile
= 1;
1008 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1009 /* overlapping locks must fail */
1010 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1011 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1012 /* non-overlapping locks must succeed */
1013 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1015 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1016 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1017 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1018 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1020 S(U(overlapped
)).Offset
= 100;
1021 S(U(overlapped
)).OffsetHigh
= 0;
1022 overlapped
.hEvent
= 0;
1024 lockfileex_capable
= dll_capable("kernel32", "LockFileEx");
1025 if (lockfileex_capable
)
1027 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1028 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1030 /* LockFileEx is probably OK, test it more. */
1031 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1032 "LockFileEx 100,100 failed\n" );
1036 /* overlapping shared locks are OK */
1037 S(U(overlapped
)).Offset
= 150;
1038 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1040 /* but exclusive is not */
1041 if (lockfileex_capable
)
1043 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1044 0, 50, 0, &overlapped
),
1045 "LockFileEx exclusive 150,50 succeeded\n" );
1046 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1048 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1049 { /* UnLockFile is capable. */
1050 S(U(overlapped
)).Offset
= 100;
1051 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1052 "UnlockFileEx 150,100 again succeeded\n" );
1057 ok( LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1058 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1059 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1060 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1062 /* wrap-around lock should not do anything */
1063 /* (but still succeeds on NT4 so we don't check result) */
1064 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1066 limited_LockFile
= 0;
1067 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1069 limited_LockFile
= 1;
1072 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1074 /* zero-byte lock */
1075 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1076 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1077 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1078 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1080 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1081 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1083 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1085 CloseHandle( handle
);
1086 DeleteFileA( filename
);
1089 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
)
1091 if (!access1
|| !access2
) return 1;
1092 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1093 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1094 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1095 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1099 static void test_file_sharing(void)
1101 static const DWORD access_modes
[4] = { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
};
1102 static const DWORD sharing_modes
[4] = { 0, FILE_SHARE_READ
, FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
};
1107 /* make sure the file exists */
1108 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1109 if (h
== INVALID_HANDLE_VALUE
)
1111 ok(0, "couldn't create file \"%s\" (err=%ld)\n", filename
, GetLastError());
1116 for (a1
= 0; a1
< 4; a1
++)
1118 for (s1
= 0; s1
< 4; s1
++)
1120 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1121 NULL
, OPEN_EXISTING
, 0, 0 );
1122 if (h
== INVALID_HANDLE_VALUE
)
1124 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename
,GetLastError());
1127 for (a2
= 0; a2
< 4; a2
++)
1129 for (s2
= 0; s2
< 4; s2
++)
1131 SetLastError(0xdeadbeef);
1132 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1133 NULL
, OPEN_EXISTING
, 0, 0 );
1134 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1135 access_modes
[a2
], sharing_modes
[s2
] ))
1137 ret
= GetLastError();
1138 ok( ERROR_SHARING_VIOLATION
== ret
|| 0 == ret
,
1139 "Windows 95 sets GetLastError() = ERROR_SHARING_VIOLATION and\n"
1140 " Windows XP GetLastError() = 0, but now it is %d.\n"
1141 " indexes = %d, %d, %d, %d\n"
1142 " modes =\n %lx/%lx/%lx/%lx\n",
1145 access_modes
[a1
], sharing_modes
[s1
],
1146 access_modes
[a2
], sharing_modes
[s2
]
1151 ok( h2
== INVALID_HANDLE_VALUE
,
1152 "open succeeded for modes %lx/%lx/%lx/%lx\n",
1153 access_modes
[a1
], sharing_modes
[s1
],
1154 access_modes
[a2
], sharing_modes
[s2
] );
1155 if (h2
== INVALID_HANDLE_VALUE
)
1156 ok( GetLastError() == ERROR_SHARING_VIOLATION
,
1157 "wrong error code %ld\n", GetLastError() );
1159 if (h2
!= INVALID_HANDLE_VALUE
) CloseHandle( h2
);
1166 SetLastError(0xdeadbeef);
1167 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1168 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %ld\n", GetLastError() );
1170 SetLastError(0xdeadbeef);
1171 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1172 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1173 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %ld\n", GetLastError() );
1175 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1176 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %ld\n", GetLastError() );
1181 DeleteFileA( filename
);
1184 static char get_windows_drive()
1186 char windowsdir
[MAX_PATH
];
1187 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1188 return windowsdir
[0];
1191 static void test_FindFirstFileA()
1194 WIN32_FIND_DATAA search_results
;
1196 char buffer
[5] = "C:\\";
1198 /* try FindFirstFileA on "C:\" */
1199 buffer
[0] = get_windows_drive();
1200 handle
= FindFirstFileA(buffer
,&search_results
);
1201 err
= GetLastError();
1202 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should Fail\n");
1203 if (handle
== INVALID_HANDLE_VALUE
)
1204 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1206 /* try FindFirstFileA on "C:\*" */
1207 strcat(buffer
, "*");
1208 handle
= FindFirstFileA(buffer
,&search_results
);
1209 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer
);
1210 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1213 static void test_FindNextFileA()
1216 WIN32_FIND_DATAA search_results
;
1218 char buffer
[5] = "C:\\*";
1220 buffer
[0] = get_windows_drive();
1221 handle
= FindFirstFileA(buffer
,&search_results
);
1222 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1223 while (FindNextFile(handle
, &search_results
))
1225 /* get to the end of the files */
1227 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1228 err
= GetLastError();
1229 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1232 static int test_Mapfile_createtemp(HANDLE
*handle
)
1234 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1235 DeleteFile(filename
);
1236 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1237 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1238 if (*handle
!= INVALID_HANDLE_VALUE
) {
1246 static void test_MapFile()
1251 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1253 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1254 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1256 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1258 /* We have to close file before we try new stuff with mapping again.
1259 Else we would always succeed on XP or block descriptors on 95. */
1260 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1261 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1262 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1263 ok( CloseHandle( handle
), "can't close file handle\n");
1266 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1268 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1269 ok( hmap
== NULL
, "mapped zero size file\n");
1270 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1272 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x1000, 0, NULL
);
1273 ok( hmap
== NULL
, "mapping should fail\n");
1274 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1276 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x1000, 0x10000, NULL
);
1277 ok( hmap
== NULL
, "mapping should fail\n");
1278 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1280 /* On XP you can now map again, on Win 95 you cannot. */
1282 ok( CloseHandle( handle
), "can't close file handle\n");
1283 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1286 static void test_GetFileType(void)
1289 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1290 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1291 type
= GetFileType(h
);
1292 ok( type
== FILE_TYPE_DISK
, "expected type disk got %ld\n", type
);
1294 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1295 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1296 type
= GetFileType(h
);
1297 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %ld\n", type
);
1299 DeleteFileA( filename
);
1302 static int completion_count
;
1304 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1306 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1307 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1311 static void test_async_file_errors(void)
1313 char szFile
[MAX_PATH
];
1314 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1316 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
1318 S(U(ovl
)).Offset
= 0;
1319 S(U(ovl
)).OffsetHigh
= 0;
1321 completion_count
= 0;
1323 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
1324 strcat(szFile
, "\\win.ini");
1325 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1326 ok(hFile
!= NULL
, "CreateFileA(%s ...) failed\n", szFile
);
1330 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
1332 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
1333 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1336 S(U(ovl
)).Offset
+= 4096;
1337 /* i/o completion routine only called if ReadFileEx returned success.
1338 * we only care about violations of this rule so undo what should have
1342 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
1343 /*printf("Error = %ld\n", GetLastError());*/
1346 static void test_read_write(void)
1350 char temp_path
[MAX_PATH
];
1351 char filename
[MAX_PATH
];
1352 static const char prefix
[] = "pfx";
1354 ret
= GetTempPathA(MAX_PATH
, temp_path
);
1355 ok(ret
!= 0, "GetTempPathA error %ld\n", GetLastError());
1356 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1358 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
1359 ok(ret
!= 0, "GetTempFileNameA error %ld\n", GetLastError());
1361 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1362 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1363 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %ld\n", GetLastError());
1365 SetLastError(12345678);
1367 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
1368 ok(ret
&& GetLastError() == 12345678,
1369 "ret = %ld, error %ld\n", ret
, GetLastError());
1370 ok(!bytes
, "bytes = %ld\n", bytes
);
1372 SetLastError(12345678);
1374 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
1375 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
1376 (ret
&& GetLastError() == 12345678), /* Win9x */
1377 "ret = %ld, error %ld\n", ret
, GetLastError());
1378 ok(!bytes
|| /* Win2k */
1379 bytes
== 10, /* Win9x */
1380 "bytes = %ld\n", bytes
);
1382 SetLastError(12345678);
1384 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
1385 ok(ret
&& GetLastError() == 12345678,
1386 "ret = %ld, error %ld\n", ret
, GetLastError());
1387 ok(!bytes
, "bytes = %ld\n", bytes
);
1389 SetLastError(12345678);
1391 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
1392 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
1393 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
1394 "ret = %ld, error %ld\n", ret
, GetLastError());
1395 ok(!bytes
, "bytes = %ld\n", bytes
);
1397 ret
= CloseHandle(hFile
);
1398 ok( ret
, "CloseHandle: error %ld\n", GetLastError());
1399 ret
= DeleteFileA(filename
);
1400 ok( ret
, "DeleteFileA: error %ld\n", GetLastError());
1413 test_GetTempFileNameA();
1422 test_FindFirstFileA();
1423 test_FindNextFileA();
1425 test_file_sharing();
1426 test_offset_in_overlapped_structure();
1429 test_async_file_errors();