2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002, 2004 Jakob Eriksson
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/test.h"
31 static int dll_capable(const char *dll
, const char *function
)
33 HMODULE module
= GetModuleHandleA(dll
);
34 if (!module
) return 0;
36 return (GetProcAddress(module
, function
) != NULL
);
39 /* keep filename and filenameW the same */
40 static const char filename
[] = "testfile.xxx";
41 static const WCHAR filenameW
[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
42 static const char sillytext
[] =
43 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
55 static void test__hread( void )
64 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
65 DeleteFileA( filename
);
66 filehandle
= _lcreat( filename
, 0 );
67 if (filehandle
== HFILE_ERROR
)
69 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
73 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
75 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
77 filehandle
= _lopen( filename
, OF_READ
);
79 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError( ) );
81 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
83 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
85 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
87 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
88 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
89 for (i
= 0; i
< bytes_wanted
; i
++)
91 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
95 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
97 ret
= DeleteFileA( filename
);
98 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
102 static void test__hwrite( void )
111 HLOCAL memory_object
;
115 filehandle
= _lcreat( filename
, 0 );
116 if (filehandle
== HFILE_ERROR
)
118 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
122 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
124 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
126 filehandle
= _lopen( filename
, OF_READ
);
128 bytes_read
= _hread( filehandle
, buffer
, 1);
130 ok( 0 == bytes_read
, "file read size error\n" );
132 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
134 filehandle
= _lopen( filename
, OF_READWRITE
);
138 srand( (unsigned)time( NULL
) );
139 for (blocks
= 0; blocks
< 100; blocks
++)
141 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
144 checksum
[0] = checksum
[0] + buffer
[i
];
146 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
147 bytes_written
= bytes_written
+ sizeof( buffer
);
150 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
153 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
155 memory_object
= LocalAlloc( LPTR
, bytes_written
);
157 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
159 contents
= LocalLock( memory_object
);
161 filehandle
= _lopen( filename
, OF_READ
);
163 contents
= LocalLock( memory_object
);
165 ok( NULL
!= contents
, "LocalLock whines\n" );
167 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
173 checksum
[0] = checksum
[0] + contents
[i
];
176 while (i
< bytes_written
- 1);
178 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
180 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
182 ret
= DeleteFileA( filename
);
183 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
187 static void test__lclose( void )
192 filehandle
= _lcreat( filename
, 0 );
193 if (filehandle
== HFILE_ERROR
)
195 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
199 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
201 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
203 ret
= DeleteFileA( filename
);
204 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
208 static void test__lcreat( void )
212 WIN32_FIND_DATAA search_results
;
213 char slashname
[] = "testfi/";
218 filehandle
= _lcreat( filename
, 0 );
219 if (filehandle
== HFILE_ERROR
)
221 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
225 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
227 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
229 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
231 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
233 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
235 ret
= DeleteFileA(filename
);
236 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
238 filehandle
= _lcreat( filename
, 1 ); /* readonly */
239 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
241 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
243 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
245 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
247 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
249 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
251 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
253 filehandle
= _lcreat( filename
, 2 );
254 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
256 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
258 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
260 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
262 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
264 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
266 ret
= DeleteFileA( filename
);
267 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
269 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
270 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
272 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
274 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
276 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
278 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
280 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
282 ret
= DeleteFileA( filename
);
283 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
285 filehandle
=_lcreat (slashname
, 0); /* illegal name */
286 if (HFILE_ERROR
==filehandle
) {
288 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
289 "creating file \"%s\" failed with error %d\n", slashname
, err
);
290 } else { /* only NT succeeds */
292 find
=FindFirstFileA (slashname
, &search_results
);
293 if (INVALID_HANDLE_VALUE
!=find
)
295 ret
= FindClose (find
);
296 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
297 slashname
[strlen(slashname
)-1]=0;
298 ok (!strcmp (slashname
, search_results
.cFileName
),
299 "found unexpected name \"%s\"\n", search_results
.cFileName
);
300 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
301 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
302 search_results
.dwFileAttributes
);
304 ret
= DeleteFileA( slashname
);
305 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
308 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
309 if (HFILE_ERROR
==filehandle
)
310 ok (0, "couldn't create volume label \"%s\"\n", filename
);
313 find
=FindFirstFileA (filename
, &search_results
);
314 if (INVALID_HANDLE_VALUE
==find
)
315 ok (0, "file \"%s\" not found\n", filename
);
317 ret
= FindClose(find
);
318 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
319 ok (!strcmp (filename
, search_results
.cFileName
),
320 "found unexpected name \"%s\"\n", search_results
.cFileName
);
321 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
322 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
323 search_results
.dwFileAttributes
);
325 ret
= DeleteFileA( filename
);
326 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
331 static void test__llseek( void )
339 filehandle
= _lcreat( filename
, 0 );
340 if (filehandle
== HFILE_ERROR
)
342 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
346 for (i
= 0; i
< 400; i
++)
348 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
350 ok( HFILE_ERROR
!= _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
), "should be able to seek\n" );
351 ok( HFILE_ERROR
!= _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
), "should be able to seek\n" );
353 bytes_read
= _hread( filehandle
, buffer
, 1);
354 ok( 1 == bytes_read
, "file read size error\n" );
355 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
356 ok( HFILE_ERROR
!= _llseek( filehandle
, -400 * strlen( sillytext
), FILE_END
), "should be able to seek\n" );
358 bytes_read
= _hread( filehandle
, buffer
, 1);
359 ok( 1 == bytes_read
, "file read size error\n" );
360 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
361 ok( HFILE_ERROR
!= _llseek( filehandle
, 1000000, FILE_END
), "should be able to seek past file; poor, poor Windows programmers\n" );
362 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
364 ret
= DeleteFileA( filename
);
365 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
369 static void test__llopen( void )
376 filehandle
= _lcreat( filename
, 0 );
377 if (filehandle
== HFILE_ERROR
)
379 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
383 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
384 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
386 filehandle
= _lopen( filename
, OF_READ
);
387 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
388 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
389 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
390 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
392 filehandle
= _lopen( filename
, OF_READWRITE
);
393 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
394 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
395 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
396 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
398 filehandle
= _lopen( filename
, OF_WRITE
);
399 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
400 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
401 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
403 ret
= DeleteFileA( filename
);
404 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
405 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
409 static void test__lread( void )
418 filehandle
= _lcreat( filename
, 0 );
419 if (filehandle
== HFILE_ERROR
)
421 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
425 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
427 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
429 filehandle
= _lopen( filename
, OF_READ
);
431 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
433 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
435 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
437 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
439 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
440 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
441 for (i
= 0; i
< bytes_wanted
; i
++)
443 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
447 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
449 ret
= DeleteFileA( filename
);
450 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
454 static void test__lwrite( void )
463 HLOCAL memory_object
;
467 filehandle
= _lcreat( filename
, 0 );
468 if (filehandle
== HFILE_ERROR
)
470 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
474 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
476 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
478 filehandle
= _lopen( filename
, OF_READ
);
480 bytes_read
= _hread( filehandle
, buffer
, 1);
482 ok( 0 == bytes_read
, "file read size error\n" );
484 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
486 filehandle
= _lopen( filename
, OF_READWRITE
);
490 srand( (unsigned)time( NULL
) );
491 for (blocks
= 0; blocks
< 100; blocks
++)
493 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
496 checksum
[0] = checksum
[0] + buffer
[i
];
498 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
499 bytes_written
= bytes_written
+ sizeof( buffer
);
502 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
505 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
507 memory_object
= LocalAlloc( LPTR
, bytes_written
);
509 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
511 contents
= LocalLock( memory_object
);
513 filehandle
= _lopen( filename
, OF_READ
);
515 contents
= LocalLock( memory_object
);
517 ok( NULL
!= contents
, "LocalLock whines\n" );
519 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
525 checksum
[0] += contents
[i
];
528 while (i
< bytes_written
- 1);
530 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
532 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
534 ret
= DeleteFileA( filename
);
535 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
538 static void test_CopyFileA(void)
540 char temp_path
[MAX_PATH
];
541 char source
[MAX_PATH
], dest
[MAX_PATH
];
542 static const char prefix
[] = "pfx";
549 ret
= GetTempPathA(MAX_PATH
, temp_path
);
550 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
551 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
553 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
554 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
556 /* make the source have not zero size */
557 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
558 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
559 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
560 ok( retok
&& ret
== sizeof(prefix
),
561 "WriteFile error %d\n", GetLastError());
562 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
563 /* get the file time and change it to prove the difference */
564 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
565 ok( ret
, "GetFileTime error %d\n", GetLastError());
566 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
567 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
568 ok( ret
, "SetFileTime error %d\n", GetLastError());
569 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
572 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
573 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
575 SetLastError(0xdeadbeef);
576 ret
= CopyFileA(source
, dest
, TRUE
);
577 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
578 "CopyFileA: unexpected error %d\n", GetLastError());
580 ret
= CopyFileA(source
, dest
, FALSE
);
581 ok(ret
, "CopyFileA: error %d\n", GetLastError());
583 /* make sure that destination has correct size */
584 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
585 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
586 ret
= GetFileSize(hfile
, NULL
);
587 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
589 /* make sure that destination has the same filetime */
590 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
591 ok( ret
, "GetFileTime error %d\n", GetLastError());
592 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
594 SetLastError(0xdeadbeef);
595 ret
= CopyFileA(source
, dest
, FALSE
);
596 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
597 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
599 /* make sure that destination still has correct size */
600 ret
= GetFileSize(hfile
, NULL
);
601 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
602 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
603 ok( retok
&& ret
== sizeof(prefix
),
604 "ReadFile: error %d\n", GetLastError());
605 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
608 ret
= DeleteFileA(source
);
609 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
610 ret
= DeleteFileA(dest
);
611 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
614 static void test_CopyFileW(void)
616 WCHAR temp_path
[MAX_PATH
];
617 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
618 static const WCHAR prefix
[] = {'p','f','x',0};
621 ret
= GetTempPathW(MAX_PATH
, temp_path
);
622 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
624 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
625 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
627 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
628 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
630 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
631 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
633 ret
= CopyFileW(source
, dest
, TRUE
);
634 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
635 "CopyFileW: unexpected error %d\n", GetLastError());
637 ret
= CopyFileW(source
, dest
, FALSE
);
638 ok(ret
, "CopyFileW: error %d\n", GetLastError());
640 ret
= DeleteFileW(source
);
641 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
642 ret
= DeleteFileW(dest
);
643 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
646 static void test_CreateFileA(void)
649 char temp_path
[MAX_PATH
];
650 char filename
[MAX_PATH
];
651 static const char prefix
[] = "pfx";
654 ret
= GetTempPathA(MAX_PATH
, temp_path
);
655 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
656 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
658 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
659 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
661 SetLastError(0xdeadbeef);
662 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
663 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
664 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
665 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
667 SetLastError(0xdeadbeef);
668 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
669 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
670 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
671 "hFile %p, last error %u\n", hFile
, GetLastError());
675 SetLastError(0xdeadbeef);
676 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
677 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
678 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
679 "hFile %p, last error %u\n", hFile
, GetLastError());
683 ret
= DeleteFileA(filename
);
684 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
686 SetLastError(0xdeadbeef);
687 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
688 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
689 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
690 "hFile %p, last error %u\n", hFile
, GetLastError());
694 ret
= DeleteFileA(filename
);
695 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
698 static void test_CreateFileW(void)
701 WCHAR temp_path
[MAX_PATH
];
702 WCHAR filename
[MAX_PATH
];
703 static const WCHAR emptyW
[]={'\0'};
704 static const WCHAR prefix
[] = {'p','f','x',0};
705 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
708 ret
= GetTempPathW(MAX_PATH
, temp_path
);
709 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
711 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
712 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
714 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
715 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
717 SetLastError(0xdeadbeef);
718 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
719 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
720 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
721 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
723 SetLastError(0xdeadbeef);
724 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
725 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
726 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
727 "hFile %p, last error %u\n", hFile
, GetLastError());
731 SetLastError(0xdeadbeef);
732 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
733 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
734 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
735 "hFile %p, last error %u\n", hFile
, GetLastError());
739 ret
= DeleteFileW(filename
);
740 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
742 SetLastError(0xdeadbeef);
743 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
744 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
745 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
746 "hFile %p, last error %u\n", hFile
, GetLastError());
750 ret
= DeleteFileW(filename
);
751 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
755 /* this crashes on NT4.0 */
756 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
757 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
758 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
759 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
762 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
763 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
764 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
765 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
767 /* test the result of opening a nonexistent driver name */
768 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
769 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
770 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
771 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
774 static void test_GetTempFileNameA(void)
778 char expected
[MAX_PATH
+ 10];
779 char windowsdir
[MAX_PATH
+ 10];
780 char windowsdrive
[3];
782 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
783 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
784 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
786 /* If the Windows directory is the root directory, it ends in backslash, not else. */
787 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
789 strcat(windowsdir
, "\\");
792 windowsdrive
[0] = windowsdir
[0];
793 windowsdrive
[1] = windowsdir
[1];
794 windowsdrive
[2] = '\0';
796 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
797 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
798 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
799 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
800 windowsdrive
[0], out
);
802 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
803 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
805 strcat(expected
, windowsdir
);
806 strcat(expected
, "abc2.tmp");
807 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
811 static void test_DeleteFileA( void )
815 ret
= DeleteFileA(NULL
);
816 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
817 GetLastError() == ERROR_PATH_NOT_FOUND
),
818 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
820 ret
= DeleteFileA("");
821 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
822 GetLastError() == ERROR_BAD_PATHNAME
),
823 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
825 ret
= DeleteFileA("nul");
826 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
827 GetLastError() == ERROR_INVALID_PARAMETER
||
828 GetLastError() == ERROR_ACCESS_DENIED
||
829 GetLastError() == ERROR_INVALID_FUNCTION
),
830 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
833 static void test_DeleteFileW( void )
836 static const WCHAR emptyW
[]={'\0'};
838 ret
= DeleteFileW(NULL
);
839 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
841 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
842 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
844 ret
= DeleteFileW(emptyW
);
845 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
846 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
849 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
851 static void test_MoveFileA(void)
853 char tempdir
[MAX_PATH
];
854 char source
[MAX_PATH
], dest
[MAX_PATH
];
855 static const char prefix
[] = "pfx";
858 ret
= GetTempPathA(MAX_PATH
, tempdir
);
859 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
860 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
862 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
863 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
865 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
866 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
868 ret
= MoveFileA(source
, dest
);
869 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
870 "MoveFileA: unexpected error %d\n", GetLastError());
872 ret
= DeleteFileA(dest
);
873 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
875 ret
= MoveFileA(source
, dest
);
876 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
878 lstrcatA(tempdir
, "Remove Me");
879 ret
= CreateDirectoryA(tempdir
, NULL
);
880 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
882 lstrcpyA(source
, dest
);
883 lstrcpyA(dest
, tempdir
);
884 lstrcatA(dest
, "\\wild?.*");
885 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
886 ret
= MoveFileA(source
, dest
);
887 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
888 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
889 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
890 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
891 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
894 char temppath
[MAX_PATH
];
897 lstrcpyA(temppath
, tempdir
);
898 lstrcatA(temppath
, "\\*.*");
899 hFind
= FindFirstFileA(temppath
, &fd
);
900 if (INVALID_HANDLE_VALUE
!= hFind
)
905 lpName
= fd
.cAlternateFileName
;
907 lpName
= fd
.cFileName
;
908 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
910 while (FindNextFileA(hFind
, &fd
));
914 ret
= DeleteFileA(source
);
915 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
916 ret
= DeleteFileA(dest
);
917 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
918 ret
= RemoveDirectoryA(tempdir
);
919 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
922 static void test_MoveFileW(void)
924 WCHAR temp_path
[MAX_PATH
];
925 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
926 static const WCHAR prefix
[] = {'p','f','x',0};
929 ret
= GetTempPathW(MAX_PATH
, temp_path
);
930 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
932 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
933 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
935 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
936 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
938 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
939 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
941 ret
= MoveFileW(source
, dest
);
942 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
943 "CopyFileW: unexpected error %d\n", GetLastError());
945 ret
= DeleteFileW(source
);
946 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
947 ret
= DeleteFileW(dest
);
948 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
951 #define PATTERN_OFFSET 0x10
953 static void test_offset_in_overlapped_structure(void)
959 BYTE buf
[256], pattern
[] = "TeSt";
961 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
964 ret
=GetTempPathA(MAX_PATH
, temp_path
);
965 ok( ret
, "GetTempPathA error %d\n", GetLastError());
966 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
967 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
969 /*** Write File *****************************************************/
971 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
972 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
974 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
975 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
976 ok( ret
, "WriteFile error %d\n", GetLastError());
977 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
979 memset(&ov
, 0, sizeof(ov
));
980 S(U(ov
)).Offset
= PATTERN_OFFSET
;
981 S(U(ov
)).OffsetHigh
= 0;
982 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
983 /* Win 9x does not support the overlapped I/O on files */
984 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
985 ok(rc
, "WriteFile error %d\n", GetLastError());
986 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
987 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
988 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
990 S(U(ov
)).Offset
= sizeof(buf
) * 2;
991 S(U(ov
)).OffsetHigh
= 0;
992 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
993 ok( ret
, "WriteFile error %d\n", GetLastError());
994 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
995 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
996 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
1001 /*** Read File *****************************************************/
1003 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1004 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
1006 memset(buf
, 0, sizeof(buf
));
1007 memset(&ov
, 0, sizeof(ov
));
1008 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1009 S(U(ov
)).OffsetHigh
= 0;
1010 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1011 /* Win 9x does not support the overlapped I/O on files */
1012 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1013 ok(rc
, "ReadFile error %d\n", GetLastError());
1014 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1015 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1016 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1017 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1022 ret
= DeleteFileA(temp_fname
);
1023 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1026 static void test_LockFile(void)
1030 OVERLAPPED overlapped
;
1031 int limited_LockFile
;
1032 int limited_UnLockFile
;
1033 int lockfileex_capable
;
1035 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1036 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1037 CREATE_ALWAYS
, 0, 0 );
1038 if (handle
== INVALID_HANDLE_VALUE
)
1040 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1043 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1045 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1046 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1048 limited_UnLockFile
= 0;
1049 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1051 limited_UnLockFile
= 1;
1054 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1055 /* overlapping locks must fail */
1056 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1057 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1058 /* non-overlapping locks must succeed */
1059 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1061 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1062 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1063 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1064 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1066 S(U(overlapped
)).Offset
= 100;
1067 S(U(overlapped
)).OffsetHigh
= 0;
1068 overlapped
.hEvent
= 0;
1070 lockfileex_capable
= dll_capable("kernel32", "LockFileEx");
1071 if (lockfileex_capable
)
1073 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1074 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1076 /* LockFileEx is probably OK, test it more. */
1077 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1078 "LockFileEx 100,100 failed\n" );
1082 /* overlapping shared locks are OK */
1083 S(U(overlapped
)).Offset
= 150;
1084 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1086 /* but exclusive is not */
1087 if (lockfileex_capable
)
1089 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1090 0, 50, 0, &overlapped
),
1091 "LockFileEx exclusive 150,50 succeeded\n" );
1092 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1094 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1095 { /* UnLockFile is capable. */
1096 S(U(overlapped
)).Offset
= 100;
1097 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1098 "UnlockFileEx 150,100 again succeeded\n" );
1103 ok( LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1104 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1105 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1106 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1108 /* wrap-around lock should not do anything */
1109 /* (but still succeeds on NT4 so we don't check result) */
1110 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1112 limited_LockFile
= 0;
1113 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1115 limited_LockFile
= 1;
1118 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1120 /* zero-byte lock */
1121 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1122 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1123 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1124 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1126 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1127 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1129 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1131 CloseHandle( handle
);
1132 DeleteFileA( filename
);
1135 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1139 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1140 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1145 if (!access1
) access1
= GENERIC_READ
;
1148 if (!access2
) access2
= GENERIC_READ
;
1151 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1152 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1153 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1154 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1155 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1156 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1160 static void test_file_sharing(void)
1162 static const DWORD access_modes
[] =
1163 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1164 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1165 static const DWORD sharing_modes
[] =
1166 { 0, FILE_SHARE_READ
,
1167 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1168 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1169 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1173 BOOL is_win9x
= FALSE
;
1175 /* make sure the file exists */
1176 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1177 if (h
== INVALID_HANDLE_VALUE
)
1179 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1182 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1185 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1187 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1189 /* Win9x doesn't support FILE_SHARE_DELETE */
1190 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1193 SetLastError(0xdeadbeef);
1194 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1195 NULL
, OPEN_EXISTING
, 0, 0 );
1196 if (h
== INVALID_HANDLE_VALUE
)
1198 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1201 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1203 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1205 /* Win9x doesn't support FILE_SHARE_DELETE */
1206 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1209 SetLastError(0xdeadbeef);
1210 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1211 NULL
, OPEN_EXISTING
, 0, 0 );
1213 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1214 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1216 ret
= GetLastError();
1218 ok( h2
!= INVALID_HANDLE_VALUE
,
1219 "open failed for modes %x/%x/%x/%x\n",
1220 access_modes
[a1
], sharing_modes
[s1
],
1221 access_modes
[a2
], sharing_modes
[s2
] );
1222 ok( ret
== 0xdeadbeef /* Win9x */ ||
1224 "wrong error code %d\n", ret
);
1230 ret
= GetLastError();
1232 ok( h2
== INVALID_HANDLE_VALUE
,
1233 "open succeeded for modes %x/%x/%x/%x\n",
1234 access_modes
[a1
], sharing_modes
[s1
],
1235 access_modes
[a2
], sharing_modes
[s2
] );
1236 ok( ret
== ERROR_SHARING_VIOLATION
,
1237 "wrong error code %d\n", ret
);
1245 SetLastError(0xdeadbeef);
1246 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1247 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1249 SetLastError(0xdeadbeef);
1250 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1251 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1252 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1254 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1255 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1260 DeleteFileA( filename
);
1263 static char get_windows_drive(void)
1265 char windowsdir
[MAX_PATH
];
1266 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1267 return windowsdir
[0];
1270 static void test_FindFirstFileA(void)
1273 WIN32_FIND_DATAA data
;
1275 char buffer
[5] = "C:\\";
1278 /* try FindFirstFileA on "C:\" */
1279 buffer
[0] = get_windows_drive();
1281 SetLastError( 0xdeadbeaf );
1282 handle
= FindFirstFileA(buffer
, &data
);
1283 err
= GetLastError();
1284 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1285 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1287 /* try FindFirstFileA on "C:\*" */
1288 strcpy(buffer2
, buffer
);
1289 strcat(buffer2
, "*");
1290 handle
= FindFirstFileA(buffer2
, &data
);
1291 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1292 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1293 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1294 if (FindNextFileA( handle
, &data
))
1295 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1296 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1297 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1299 /* try FindFirstFileA on windows dir */
1300 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1301 strcat(buffer2
, "\\*");
1302 handle
= FindFirstFileA(buffer2
, &data
);
1303 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1304 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1305 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1306 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1307 while (FindNextFileA( handle
, &data
))
1308 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1309 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1310 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1312 /* try FindFirstFileA on "C:\foo\" */
1313 SetLastError( 0xdeadbeaf );
1314 strcpy(buffer2
, buffer
);
1315 strcat(buffer2
, "foo\\");
1316 handle
= FindFirstFileA(buffer2
, &data
);
1317 err
= GetLastError();
1318 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1320 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1323 /* try FindFirstFileA on "C:\foo\bar.txt" */
1324 SetLastError( 0xdeadbeaf );
1325 strcpy(buffer2
, buffer
);
1326 strcat(buffer2
, "foo\\bar.txt");
1327 handle
= FindFirstFileA(buffer2
, &data
);
1328 err
= GetLastError();
1329 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1330 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1332 /* try FindFirstFileA on "C:\foo\*.*" */
1333 SetLastError( 0xdeadbeaf );
1334 strcpy(buffer2
, buffer
);
1335 strcat(buffer2
, "foo\\*.*");
1336 handle
= FindFirstFileA(buffer2
, &data
);
1337 err
= GetLastError();
1338 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1339 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1341 /* try FindFirstFileA on "foo\bar.txt" */
1342 SetLastError( 0xdeadbeaf );
1343 strcpy(buffer2
, "foo\\bar.txt");
1344 handle
= FindFirstFileA(buffer2
, &data
);
1345 err
= GetLastError();
1346 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1347 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1349 /* try FindFirstFileA on "c:\nul" */
1350 SetLastError( 0xdeadbeaf );
1351 strcpy(buffer2
, buffer
);
1352 strcat(buffer2
, "nul");
1353 handle
= FindFirstFileA(buffer2
, &data
);
1354 err
= GetLastError();
1355 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1356 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
1357 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1358 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1359 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
, "wrong attributes %x\n", data
.dwFileAttributes
);
1360 SetLastError( 0xdeadbeaf );
1361 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1362 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1363 ok( FindClose( handle
), "failed to close handle\n" );
1365 /* try FindFirstFileA on "lpt1" */
1366 SetLastError( 0xdeadbeaf );
1367 strcpy(buffer2
, "lpt1");
1368 handle
= FindFirstFileA(buffer2
, &data
);
1369 err
= GetLastError();
1370 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1371 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
1372 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1373 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1374 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
, "wrong attributes %x\n", data
.dwFileAttributes
);
1375 SetLastError( 0xdeadbeaf );
1376 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1377 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1378 ok( FindClose( handle
), "failed to close handle\n" );
1380 /* try FindFirstFileA on "c:\nul\*" */
1381 SetLastError( 0xdeadbeaf );
1382 strcpy(buffer2
, buffer
);
1383 strcat(buffer2
, "nul\\*");
1384 handle
= FindFirstFileA(buffer2
, &data
);
1385 err
= GetLastError();
1386 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1387 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1389 /* try FindFirstFileA on "c:\nul*" */
1390 SetLastError( 0xdeadbeaf );
1391 strcpy(buffer2
, buffer
);
1392 strcat(buffer2
, "nul*");
1393 handle
= FindFirstFileA(buffer2
, &data
);
1394 err
= GetLastError();
1395 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1396 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1398 /* try FindFirstFileA on "c:\foo\bar\nul" */
1399 SetLastError( 0xdeadbeaf );
1400 strcpy(buffer2
, buffer
);
1401 strcat(buffer2
, "foo\\bar\\nul");
1402 handle
= FindFirstFileA(buffer2
, &data
);
1403 err
= GetLastError();
1404 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1405 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1407 /* try FindFirstFileA on "c:\foo\nul\bar" */
1408 SetLastError( 0xdeadbeaf );
1409 strcpy(buffer2
, buffer
);
1410 strcat(buffer2
, "foo\\nul\\bar");
1411 handle
= FindFirstFileA(buffer2
, &data
);
1412 err
= GetLastError();
1413 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1414 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1417 static void test_FindNextFileA(void)
1420 WIN32_FIND_DATAA search_results
;
1422 char buffer
[5] = "C:\\*";
1424 buffer
[0] = get_windows_drive();
1425 handle
= FindFirstFileA(buffer
,&search_results
);
1426 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1427 while (FindNextFile(handle
, &search_results
))
1429 /* get to the end of the files */
1431 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1432 err
= GetLastError();
1433 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1436 static void test_FindFirstFileExA(void)
1438 WIN32_FIND_DATAA search_results
;
1441 CreateDirectoryA("test-dir", NULL
);
1442 _lclose(_lcreat("test-dir\\file1", 0));
1443 _lclose(_lcreat("test-dir\\file2", 0));
1444 CreateDirectoryA("test-dir\\dir1", NULL
);
1445 /* FindExLimitToDirectories is ignored */
1446 handle
= FindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, FindExSearchLimitToDirectories
, NULL
, 0);
1447 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
1448 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
1450 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1452 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
1453 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
1455 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
1456 ok(CHECK_NAME(search_results
.cFileName
), "Invalid thrid entry - %s\n", search_results
.cFileName
);
1458 ok(FindNextFile(handle
, &search_results
), "Fetching fourth file failed\n");
1459 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1461 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1462 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1466 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should failed\n");
1467 DeleteFileA("test-dir\\file1");
1468 DeleteFileA("test-dir\\file2");
1469 RemoveDirectoryA("test-dir\\dir1");
1470 RemoveDirectoryA("test-dir");
1473 static int test_Mapfile_createtemp(HANDLE
*handle
)
1475 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1476 DeleteFile(filename
);
1477 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1478 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1479 if (*handle
!= INVALID_HANDLE_VALUE
) {
1487 static void test_MapFile(void)
1492 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1494 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1495 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1497 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1499 /* We have to close file before we try new stuff with mapping again.
1500 Else we would always succeed on XP or block descriptors on 95. */
1501 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1502 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1503 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1504 ok( CloseHandle( handle
), "can't close file handle\n");
1507 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1509 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1510 ok( hmap
== NULL
, "mapped zero size file\n");
1511 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1513 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1514 ok( hmap
== NULL
, "mapping should fail\n");
1515 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1517 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1518 ok( hmap
== NULL
, "mapping should fail\n");
1519 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1521 /* On XP you can now map again, on Win 95 you cannot. */
1523 ok( CloseHandle( handle
), "can't close file handle\n");
1524 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1527 static void test_GetFileType(void)
1530 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1531 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1532 type
= GetFileType(h
);
1533 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1535 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1536 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1537 type
= GetFileType(h
);
1538 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1540 DeleteFileA( filename
);
1543 static int completion_count
;
1545 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1547 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1548 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1552 static void test_async_file_errors(void)
1554 char szFile
[MAX_PATH
];
1555 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1557 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
1559 S(U(ovl
)).Offset
= 0;
1560 S(U(ovl
)).OffsetHigh
= 0;
1562 completion_count
= 0;
1564 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
1565 strcat(szFile
, "\\win.ini");
1566 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1567 ok(hFile
!= NULL
, "CreateFileA(%s ...) failed\n", szFile
);
1571 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
1573 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
1574 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1577 S(U(ovl
)).Offset
+= 4096;
1578 /* i/o completion routine only called if ReadFileEx returned success.
1579 * we only care about violations of this rule so undo what should have
1583 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
1584 /*printf("Error = %ld\n", GetLastError());*/
1587 static void test_read_write(void)
1591 char temp_path
[MAX_PATH
];
1592 char filename
[MAX_PATH
];
1593 static const char prefix
[] = "pfx";
1595 ret
= GetTempPathA(MAX_PATH
, temp_path
);
1596 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1597 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1599 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
1600 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1602 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1603 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1604 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
1606 SetLastError(12345678);
1608 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
1609 ok(ret
&& GetLastError() == 12345678,
1610 "ret = %d, error %d\n", ret
, GetLastError());
1611 ok(!bytes
, "bytes = %d\n", bytes
);
1613 SetLastError(12345678);
1615 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
1616 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
1617 (ret
&& GetLastError() == 12345678), /* Win9x */
1618 "ret = %d, error %d\n", ret
, GetLastError());
1619 ok(!bytes
|| /* Win2k */
1620 bytes
== 10, /* Win9x */
1621 "bytes = %d\n", bytes
);
1623 /* make sure the file contains data */
1624 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
1625 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
1627 SetLastError(12345678);
1629 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
1630 ok(ret
&& GetLastError() == 12345678,
1631 "ret = %d, error %d\n", ret
, GetLastError());
1632 ok(!bytes
, "bytes = %d\n", bytes
);
1634 SetLastError(12345678);
1636 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
1637 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
1638 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
1639 "ret = %d, error %d\n", ret
, GetLastError());
1640 ok(!bytes
, "bytes = %d\n", bytes
);
1642 ret
= CloseHandle(hFile
);
1643 ok( ret
, "CloseHandle: error %d\n", GetLastError());
1644 ret
= DeleteFileA(filename
);
1645 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
1648 static void test_OpenFile(void)
1655 static const char *file
= "\\regsvr32.exe";
1656 static const char *foo
= ".\\foo-bar-foo.baz";
1657 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
1658 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1659 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1660 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1661 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1662 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1663 static const char *backslash
= "\\";
1664 char buff
[MAX_PATH
];
1665 char buff_long
[4*MAX_PATH
];
1666 char filled_0xA5
[OFS_MAXPATHNAME
];
1669 /* Check for existing file */
1670 length
= GetSystemDirectoryA(buff
, MAX_PATH
);
1672 if (length
+ lstrlen(file
) < MAX_PATH
)
1674 lstrcatA(buff
, file
);
1675 memset(&ofs
, 0xA5, sizeof(ofs
));
1676 SetLastError(0xfaceabee);
1678 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
1679 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
1680 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1681 "GetLastError() returns %d\n", GetLastError() );
1682 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1683 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1684 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1685 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1686 ofs
.szPathName
, buff
);
1689 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
1690 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1692 /* Check for nonexistent file */
1693 if (length
+ lstrlenA(foo
+ 1) < MAX_PATH
)
1695 lstrcatA(buff
, foo
+ 1); /* Avoid '.' during concatenation */
1696 memset(&ofs
, 0xA5, sizeof(ofs
));
1697 SetLastError(0xfaceabee);
1699 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
1700 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1701 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
1703 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1704 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1705 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1706 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1707 ofs
.szPathName
, buff
);
1710 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
1711 length
+= lstrlenA(foo_too_long
+ 1);
1713 /* Check for nonexistent file with too long filename */
1714 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
1716 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
1717 memset(&ofs
, 0xA5, sizeof(ofs
));
1718 SetLastError(0xfaceabee);
1720 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
1721 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1722 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
1723 "GetLastError() returns %d\n", GetLastError() );
1725 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1726 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
1727 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1728 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1729 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1733 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1734 length
+= lstrlenA(backslash
);
1735 length
+= lstrlenA(filename
);
1737 if (length
>= MAX_PATH
)
1739 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
1742 lstrcatA(buff
, backslash
);
1743 lstrcatA(buff
, filename
);
1745 memset(&ofs
, 0xA5, sizeof(ofs
));
1746 SetLastError(0xfaceabee);
1747 /* Create an empty file */
1748 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
1749 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
1750 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1751 "GetLastError() returns %d\n", GetLastError() );
1752 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1753 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1754 ret
= CloseHandle((HANDLE
)hFile
);
1755 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1756 retval
= GetFileAttributesA(filename
);
1757 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
1759 memset(&ofs
, 0xA5, sizeof(ofs
));
1760 SetLastError(0xfaceabee);
1761 /* Check various opening options: */
1762 /* for reading only, */
1763 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
1764 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
1765 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1766 "GetLastError() returns %d\n", GetLastError() );
1767 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1768 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1769 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1770 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1771 ret
= CloseHandle((HANDLE
)hFile
);
1772 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1774 memset(&ofs
, 0xA5, sizeof(ofs
));
1775 SetLastError(0xfaceabee);
1776 /* for writing only, */
1777 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
1778 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
1779 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1780 "GetLastError() returns %d\n", GetLastError() );
1781 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1782 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1783 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1784 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1785 ret
= CloseHandle((HANDLE
)hFile
);
1786 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1788 memset(&ofs
, 0xA5, sizeof(ofs
));
1789 SetLastError(0xfaceabee);
1790 /* for reading and writing, */
1791 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
1792 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
1793 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1794 "GetLastError() returns %d\n", GetLastError() );
1795 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1796 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1797 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1798 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1799 ret
= CloseHandle((HANDLE
)hFile
);
1800 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1802 memset(&ofs
, 0xA5, sizeof(ofs
));
1803 SetLastError(0xfaceabee);
1804 /* for checking file presence. */
1805 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
1806 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
1807 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1808 "GetLastError() returns %d\n", GetLastError() );
1809 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1810 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1811 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1812 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1814 memset(&ofs
, 0xA5, sizeof(ofs
));
1815 SetLastError(0xfaceabee);
1816 /* Delete the file and make sure it doesn't exist anymore */
1817 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
1818 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
1819 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1820 "GetLastError() returns %d\n", GetLastError() );
1821 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1822 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1823 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1824 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1826 retval
= GetFileAttributesA(filename
);
1827 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
1830 static void test_overlapped(void)
1835 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1837 memset( &ov
, 0, sizeof ov
);
1839 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1840 ok( r
== TRUE
, "should return false\n");
1841 ok( result
== 0, "wrong result %u\n", result
);
1845 ov
.InternalHigh
= 0xabcd;
1846 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1847 ok( r
== TRUE
, "should return false\n");
1848 ok( result
== 0xabcd, "wrong result %u\n", result
);
1850 SetLastError( 0xb00 );
1852 ov
.Internal
= STATUS_INVALID_HANDLE
;
1853 ov
.InternalHigh
= 0xabcd;
1854 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1855 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
1856 ok( r
== FALSE
, "should return false\n");
1857 ok( result
== 0xabcd, "wrong result %u\n", result
);
1859 SetLastError( 0xb00 );
1861 ov
.Internal
= STATUS_PENDING
;
1862 ov
.InternalHigh
= 0xabcd;
1863 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1864 ok( GetLastError() == ERROR_IO_INCOMPLETE
, "wrong error %u\n", GetLastError() );
1865 ok( r
== FALSE
, "should return false\n");
1866 ok( result
== 0, "wrong result %u\n", result
);
1868 SetLastError( 0xb00 );
1869 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
1870 ov
.Internal
= STATUS_PENDING
;
1871 ov
.InternalHigh
= 0xabcd;
1872 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1873 ok( GetLastError() == ERROR_IO_INCOMPLETE
, "wrong error %u\n", GetLastError() );
1874 ok( r
== FALSE
, "should return false\n");
1876 ResetEvent( ov
.hEvent
);
1878 SetLastError( 0xb00 );
1879 ov
.Internal
= STATUS_PENDING
;
1880 ov
.InternalHigh
= 0;
1881 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1882 ok( GetLastError() == ERROR_IO_INCOMPLETE
, "wrong error %u\n", GetLastError() );
1883 ok( r
== FALSE
, "should return false\n");
1885 r
= CloseHandle( ov
.hEvent
);
1886 ok( r
== TRUE
, "close handle failed\n");
1889 static void test_RemoveDirectory(void)
1892 char directory
[] = "removeme";
1894 rc
= CreateDirectory(directory
, NULL
);
1895 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
1897 rc
= SetCurrentDirectory(directory
);
1898 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1900 rc
= RemoveDirectory(".");
1902 ok( !rc
, "RemoveDirectory unexpectedly worked\n" );
1905 rc
= SetCurrentDirectory("..");
1906 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1908 rc
= RemoveDirectory(directory
);
1910 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1924 test_GetTempFileNameA();
1933 test_FindFirstFileA();
1934 test_FindNextFileA();
1935 test_FindFirstFileExA();
1937 test_file_sharing();
1938 test_offset_in_overlapped_structure();
1941 test_async_file_errors();
1945 test_RemoveDirectory();