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 /* keep filename and filenameW the same */
32 static const char filename
[] = "testfile.xxx";
33 static const WCHAR filenameW
[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
34 static const char sillytext
[] =
35 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
36 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
37 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
38 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
39 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
40 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
41 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
42 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
43 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
44 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
47 static void test__hread( void )
56 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
); /* be sure to remove stale files */
57 DeleteFileA( filename
);
58 filehandle
= _lcreat( filename
, 0 );
59 if (filehandle
== HFILE_ERROR
)
61 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
65 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
67 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
69 filehandle
= _lopen( filename
, OF_READ
);
71 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError( ) );
73 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
75 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
77 for (bytes_wanted
= 0; bytes_wanted
< lstrlenA( sillytext
); bytes_wanted
++)
79 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
80 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
81 for (i
= 0; i
< bytes_wanted
; i
++)
83 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
87 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
89 ret
= DeleteFileA( filename
);
90 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
94 static void test__hwrite( void )
103 HLOCAL memory_object
;
107 filehandle
= _lcreat( filename
, 0 );
108 if (filehandle
== HFILE_ERROR
)
110 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
114 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
116 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
118 filehandle
= _lopen( filename
, OF_READ
);
120 bytes_read
= _hread( filehandle
, buffer
, 1);
122 ok( 0 == bytes_read
, "file read size error\n" );
124 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
126 filehandle
= _lopen( filename
, OF_READWRITE
);
130 srand( (unsigned)time( NULL
) );
131 for (blocks
= 0; blocks
< 100; blocks
++)
133 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
136 checksum
[0] = checksum
[0] + buffer
[i
];
138 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
139 bytes_written
= bytes_written
+ sizeof( buffer
);
142 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
145 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
147 memory_object
= LocalAlloc( LPTR
, bytes_written
);
149 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)\n" );
151 contents
= LocalLock( memory_object
);
153 filehandle
= _lopen( filename
, OF_READ
);
155 contents
= LocalLock( memory_object
);
157 ok( NULL
!= contents
, "LocalLock whines\n" );
159 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
165 checksum
[0] = checksum
[0] + contents
[i
];
168 while (i
< bytes_written
- 1);
170 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
172 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
174 ret
= DeleteFileA( filename
);
175 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
179 static void test__lclose( void )
184 filehandle
= _lcreat( filename
, 0 );
185 if (filehandle
== HFILE_ERROR
)
187 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
191 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
193 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
195 ret
= DeleteFileA( filename
);
196 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError( ) );
200 static void test__lcreat( void )
204 WIN32_FIND_DATAA search_results
;
205 char slashname
[] = "testfi/";
210 filehandle
= _lcreat( filename
, 0 );
211 if (filehandle
== HFILE_ERROR
)
213 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
217 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
219 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
221 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
223 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
225 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
227 ret
= DeleteFileA(filename
);
228 ok( ret
!= 0, "DeleteFile failed (%d)\n", GetLastError());
230 filehandle
= _lcreat( filename
, 1 ); /* readonly */
231 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
233 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less\n" );
235 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
237 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file\n" );
239 ok( 0 == DeleteFileA( filename
), "shouldn't be able to delete a readonly file\n" );
241 ok( SetFileAttributesA(filename
, FILE_ATTRIBUTE_NORMAL
) != 0, "couldn't change attributes on file\n" );
243 ok( DeleteFileA( filename
) != 0, "now it should be possible to delete the file!\n" );
245 filehandle
= _lcreat( filename
, 2 );
246 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
248 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
250 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
252 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
254 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
256 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
258 ret
= DeleteFileA( filename
);
259 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
261 filehandle
= _lcreat( filename
, 4 ); /* SYSTEM file */
262 ok( HFILE_ERROR
!= filehandle
, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError( ) );
264 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
266 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
268 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == lstrlenA( sillytext
), "erratic _hread return value\n" );
270 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
272 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should STILL be able to find file\n" );
274 ret
= DeleteFileA( filename
);
275 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
277 filehandle
=_lcreat (slashname
, 0); /* illegal name */
278 if (HFILE_ERROR
==filehandle
) {
280 ok (err
==ERROR_INVALID_NAME
|| err
==ERROR_PATH_NOT_FOUND
,
281 "creating file \"%s\" failed with error %d\n", slashname
, err
);
282 } else { /* only NT succeeds */
284 find
=FindFirstFileA (slashname
, &search_results
);
285 if (INVALID_HANDLE_VALUE
!=find
)
287 ret
= FindClose (find
);
288 ok (0 != ret
, "FindClose complains (%d)\n", GetLastError ());
289 slashname
[strlen(slashname
)-1]=0;
290 ok (!strcmp (slashname
, search_results
.cFileName
),
291 "found unexpected name \"%s\"\n", search_results
.cFileName
);
292 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
293 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
294 search_results
.dwFileAttributes
);
296 ret
= DeleteFileA( slashname
);
297 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
300 filehandle
=_lcreat (filename
, 8); /* illegal attribute */
301 if (HFILE_ERROR
==filehandle
)
302 ok (0, "couldn't create volume label \"%s\"\n", filename
);
305 find
=FindFirstFileA (filename
, &search_results
);
306 if (INVALID_HANDLE_VALUE
==find
)
307 ok (0, "file \"%s\" not found\n", filename
);
309 ret
= FindClose(find
);
310 ok ( 0 != ret
, "FindClose complains (%d)\n", GetLastError ());
311 ok (!strcmp (filename
, search_results
.cFileName
),
312 "found unexpected name \"%s\"\n", search_results
.cFileName
);
313 search_results
.dwFileAttributes
&= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
;
314 ok (FILE_ATTRIBUTE_ARCHIVE
==search_results
.dwFileAttributes
,
315 "attributes of file \"%s\" are 0x%04x\n", search_results
.cFileName
,
316 search_results
.dwFileAttributes
);
318 ret
= DeleteFileA( filename
);
319 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
324 static void test__llseek( void )
332 filehandle
= _lcreat( filename
, 0 );
333 if (filehandle
== HFILE_ERROR
)
335 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
339 for (i
= 0; i
< 400; i
++)
341 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
343 ok( HFILE_ERROR
!= _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
), "should be able to seek\n" );
344 ok( HFILE_ERROR
!= _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
), "should be able to seek\n" );
346 bytes_read
= _hread( filehandle
, buffer
, 1);
347 ok( 1 == bytes_read
, "file read size error\n" );
348 ok( buffer
[0] == sillytext
[27], "_llseek error, it got lost seeking\n" );
349 ok( HFILE_ERROR
!= _llseek( filehandle
, -400 * strlen( sillytext
), FILE_END
), "should be able to seek\n" );
351 bytes_read
= _hread( filehandle
, buffer
, 1);
352 ok( 1 == bytes_read
, "file read size error\n" );
353 ok( buffer
[0] == sillytext
[0], "_llseek error, it got lost seeking\n" );
354 ok( HFILE_ERROR
!= _llseek( filehandle
, 1000000, FILE_END
), "should be able to seek past file; poor, poor Windows programmers\n" );
355 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
357 ret
= DeleteFileA( filename
);
358 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
362 static void test__llopen( void )
369 filehandle
= _lcreat( filename
, 0 );
370 if (filehandle
== HFILE_ERROR
)
372 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
376 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
377 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
379 filehandle
= _lopen( filename
, OF_READ
);
380 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!\n" );
381 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
382 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
383 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
385 filehandle
= _lopen( filename
, OF_READWRITE
);
386 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
387 ok( strlen( sillytext
) == bytes_read
, "file read size error\n" );
388 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
389 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
391 filehandle
= _lopen( filename
, OF_WRITE
);
392 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file\n" );
393 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine\n" );
394 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
396 ret
= DeleteFileA( filename
);
397 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
398 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
402 static void test__lread( void )
411 filehandle
= _lcreat( filename
, 0 );
412 if (filehandle
== HFILE_ERROR
)
414 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
418 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains\n" );
420 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
422 filehandle
= _lopen( filename
, OF_READ
);
424 ok( HFILE_ERROR
!= filehandle
, "couldn't open file \"%s\" again (err=%d)\n", filename
, GetLastError());
426 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
428 ok( lstrlenA( sillytext
) == bytes_read
, "file read size error\n" );
430 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
432 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains\n" );
433 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value\n" );
434 for (i
= 0; i
< bytes_wanted
; i
++)
436 ok( buffer
[i
] == sillytext
[i
], "that's not what's written\n" );
440 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
442 ret
= DeleteFileA( filename
);
443 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
447 static void test__lwrite( void )
456 HLOCAL memory_object
;
460 filehandle
= _lcreat( filename
, 0 );
461 if (filehandle
== HFILE_ERROR
)
463 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
467 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains\n" );
469 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
471 filehandle
= _lopen( filename
, OF_READ
);
473 bytes_read
= _hread( filehandle
, buffer
, 1);
475 ok( 0 == bytes_read
, "file read size error\n" );
477 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains\n" );
479 filehandle
= _lopen( filename
, OF_READWRITE
);
483 srand( (unsigned)time( NULL
) );
484 for (blocks
= 0; blocks
< 100; blocks
++)
486 for (i
= 0; i
< (long)sizeof( buffer
); i
++)
489 checksum
[0] = checksum
[0] + buffer
[i
];
491 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains\n" );
492 bytes_written
= bytes_written
+ sizeof( buffer
);
495 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains\n" );
498 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
500 memory_object
= LocalAlloc( LPTR
, bytes_written
);
502 ok( 0 != memory_object
, "LocalAlloc fails, could be out of memory\n" );
504 contents
= LocalLock( memory_object
);
506 filehandle
= _lopen( filename
, OF_READ
);
508 contents
= LocalLock( memory_object
);
510 ok( NULL
!= contents
, "LocalLock whines\n" );
512 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length\n" );
518 checksum
[0] += contents
[i
];
521 while (i
< bytes_written
- 1);
523 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum\n" );
525 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains\n" );
527 ret
= DeleteFileA( filename
);
528 ok( ret
, "DeleteFile failed (%d)\n", GetLastError( ) );
531 static void test_CopyFileA(void)
533 char temp_path
[MAX_PATH
];
534 char source
[MAX_PATH
], dest
[MAX_PATH
];
535 static const char prefix
[] = "pfx";
542 ret
= GetTempPathA(MAX_PATH
, temp_path
);
543 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
544 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
546 ret
= GetTempFileNameA(temp_path
, prefix
, 0, source
);
547 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
549 /* make the source have not zero size */
550 hfile
= CreateFileA(source
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
551 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open source file\n");
552 retok
= WriteFile(hfile
, prefix
, sizeof(prefix
), &ret
, NULL
);
553 ok( retok
&& ret
== sizeof(prefix
),
554 "WriteFile error %d\n", GetLastError());
555 ok(GetFileSize(hfile
, NULL
) == sizeof(prefix
), "source file has wrong size\n");
556 /* get the file time and change it to prove the difference */
557 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft1
);
558 ok( ret
, "GetFileTime error %d\n", GetLastError());
559 ft1
.dwLowDateTime
-= 600000000; /* 60 second */
560 ret
= SetFileTime(hfile
, NULL
, NULL
, &ft1
);
561 ok( ret
, "SetFileTime error %d\n", GetLastError());
562 GetFileTime(hfile
, NULL
, NULL
, &ft1
); /* get the actual time back */
565 ret
= GetTempFileNameA(temp_path
, prefix
, 0, dest
);
566 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
568 SetLastError(0xdeadbeef);
569 ret
= CopyFileA(source
, dest
, TRUE
);
570 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
571 "CopyFileA: unexpected error %d\n", GetLastError());
573 ret
= CopyFileA(source
, dest
, FALSE
);
574 ok(ret
, "CopyFileA: error %d\n", GetLastError());
576 /* make sure that destination has correct size */
577 hfile
= CreateFileA(dest
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
578 ok(hfile
!= INVALID_HANDLE_VALUE
, "failed to open destination file\n");
579 ret
= GetFileSize(hfile
, NULL
);
580 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
582 /* make sure that destination has the same filetime */
583 ret
= GetFileTime(hfile
, NULL
, NULL
, &ft2
);
584 ok( ret
, "GetFileTime error %d\n", GetLastError());
585 ok(CompareFileTime(&ft1
, &ft2
) == 0, "destination file has wrong filetime\n");
587 SetLastError(0xdeadbeef);
588 ret
= CopyFileA(source
, dest
, FALSE
);
589 ok(!ret
&& GetLastError() == ERROR_SHARING_VIOLATION
,
590 "CopyFileA: ret = %d, unexpected error %d\n", ret
, GetLastError());
592 /* make sure that destination still has correct size */
593 ret
= GetFileSize(hfile
, NULL
);
594 ok(ret
== sizeof(prefix
), "destination file has wrong size %d\n", ret
);
595 retok
= ReadFile(hfile
, buf
, sizeof(buf
), &ret
, NULL
);
596 ok( retok
&& ret
== sizeof(prefix
),
597 "ReadFile: error %d\n", GetLastError());
598 ok(!memcmp(prefix
, buf
, sizeof(prefix
)), "buffer contents mismatch\n");
601 ret
= DeleteFileA(source
);
602 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
603 ret
= DeleteFileA(dest
);
604 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
607 static void test_CopyFileW(void)
609 WCHAR temp_path
[MAX_PATH
];
610 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
611 static const WCHAR prefix
[] = {'p','f','x',0};
614 ret
= GetTempPathW(MAX_PATH
, temp_path
);
615 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
617 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
618 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
620 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
621 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
623 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
624 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
626 ret
= CopyFileW(source
, dest
, TRUE
);
627 ok(!ret
&& GetLastError() == ERROR_FILE_EXISTS
,
628 "CopyFileW: unexpected error %d\n", GetLastError());
630 ret
= CopyFileW(source
, dest
, FALSE
);
631 ok(ret
, "CopyFileW: error %d\n", GetLastError());
633 ret
= DeleteFileW(source
);
634 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
635 ret
= DeleteFileW(dest
);
636 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
639 static void test_CreateFileA(void)
642 char temp_path
[MAX_PATH
];
643 char filename
[MAX_PATH
];
644 static const char prefix
[] = "pfx";
647 ret
= GetTempPathA(MAX_PATH
, temp_path
);
648 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
649 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
651 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
652 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
654 SetLastError(0xdeadbeef);
655 hFile
= CreateFileA(filename
, GENERIC_READ
, 0, NULL
,
656 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
657 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
658 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
660 SetLastError(0xdeadbeef);
661 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
662 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
663 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
664 "hFile %p, last error %u\n", hFile
, GetLastError());
668 SetLastError(0xdeadbeef);
669 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
670 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
671 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
672 "hFile %p, last error %u\n", hFile
, GetLastError());
676 ret
= DeleteFileA(filename
);
677 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
679 SetLastError(0xdeadbeef);
680 hFile
= CreateFileA(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
681 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
682 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
683 "hFile %p, last error %u\n", hFile
, GetLastError());
687 ret
= DeleteFileA(filename
);
688 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
691 static void test_CreateFileW(void)
694 WCHAR temp_path
[MAX_PATH
];
695 WCHAR filename
[MAX_PATH
];
696 static const WCHAR emptyW
[]={'\0'};
697 static const WCHAR prefix
[] = {'p','f','x',0};
698 static const WCHAR bogus
[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
701 ret
= GetTempPathW(MAX_PATH
, temp_path
);
702 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
704 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
705 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
707 ret
= GetTempFileNameW(temp_path
, prefix
, 0, filename
);
708 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
710 SetLastError(0xdeadbeef);
711 hFile
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
,
712 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
713 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_EXISTS
,
714 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
716 SetLastError(0xdeadbeef);
717 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
718 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
719 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
720 "hFile %p, last error %u\n", hFile
, GetLastError());
724 SetLastError(0xdeadbeef);
725 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
726 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
727 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_ALREADY_EXISTS
,
728 "hFile %p, last error %u\n", hFile
, GetLastError());
732 ret
= DeleteFileW(filename
);
733 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
735 SetLastError(0xdeadbeef);
736 hFile
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
737 OPEN_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
738 ok(hFile
!= INVALID_HANDLE_VALUE
&& GetLastError() == 0,
739 "hFile %p, last error %u\n", hFile
, GetLastError());
743 ret
= DeleteFileW(filename
);
744 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
748 /* this crashes on NT4.0 */
749 hFile
= CreateFileW(NULL
, GENERIC_READ
, 0, NULL
,
750 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
751 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
752 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile
,GetLastError());
755 hFile
= CreateFileW(emptyW
, GENERIC_READ
, 0, NULL
,
756 CREATE_NEW
, FILE_FLAG_RANDOM_ACCESS
, 0);
757 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
758 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile
,GetLastError());
760 /* test the result of opening a nonexistent driver name */
761 hFile
= CreateFileW(bogus
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
762 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
763 ok(hFile
== INVALID_HANDLE_VALUE
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
764 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile
,GetLastError());
767 static void test_GetTempFileNameA(void)
771 char expected
[MAX_PATH
+ 10];
772 char windowsdir
[MAX_PATH
+ 10];
773 char windowsdrive
[3];
775 result
= GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
776 ok(result
< sizeof(windowsdir
), "windowsdir is abnormally long!\n");
777 ok(result
!= 0, "GetWindowsDirectory: error %d\n", GetLastError());
779 /* If the Windows directory is the root directory, it ends in backslash, not else. */
780 if (strlen(windowsdir
) != 3) /* As in "C:\" or "F:\" */
782 strcat(windowsdir
, "\\");
785 windowsdrive
[0] = windowsdir
[0];
786 windowsdrive
[1] = windowsdir
[1];
787 windowsdrive
[2] = '\0';
789 result
= GetTempFileNameA(windowsdrive
, "abc", 1, out
);
790 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
791 ok(((out
[0] == windowsdrive
[0]) && (out
[1] == ':')) && (out
[2] == '\\'),
792 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
793 windowsdrive
[0], out
);
795 result
= GetTempFileNameA(windowsdir
, "abc", 2, out
);
796 ok(result
!= 0, "GetTempFileNameA: error %d\n", GetLastError());
798 strcat(expected
, windowsdir
);
799 strcat(expected
, "abc2.tmp");
800 ok(lstrcmpiA(out
, expected
) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
804 static void test_DeleteFileA( void )
808 ret
= DeleteFileA(NULL
);
809 ok(!ret
&& (GetLastError() == ERROR_INVALID_PARAMETER
||
810 GetLastError() == ERROR_PATH_NOT_FOUND
),
811 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
813 ret
= DeleteFileA("");
814 ok(!ret
&& (GetLastError() == ERROR_PATH_NOT_FOUND
||
815 GetLastError() == ERROR_BAD_PATHNAME
),
816 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
818 ret
= DeleteFileA("nul");
819 ok(!ret
&& (GetLastError() == ERROR_FILE_NOT_FOUND
||
820 GetLastError() == ERROR_INVALID_PARAMETER
||
821 GetLastError() == ERROR_ACCESS_DENIED
||
822 GetLastError() == ERROR_INVALID_FUNCTION
),
823 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret
,GetLastError());
826 static void test_DeleteFileW( void )
829 static const WCHAR emptyW
[]={'\0'};
831 ret
= DeleteFileW(NULL
);
832 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
834 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
835 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret
,GetLastError());
837 ret
= DeleteFileW(emptyW
);
838 ok(!ret
&& GetLastError() == ERROR_PATH_NOT_FOUND
,
839 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret
,GetLastError());
842 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
844 static void test_MoveFileA(void)
846 char tempdir
[MAX_PATH
];
847 char source
[MAX_PATH
], dest
[MAX_PATH
];
848 static const char prefix
[] = "pfx";
851 ret
= GetTempPathA(MAX_PATH
, tempdir
);
852 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
853 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
855 ret
= GetTempFileNameA(tempdir
, prefix
, 0, source
);
856 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
858 ret
= GetTempFileNameA(tempdir
, prefix
, 0, dest
);
859 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
861 ret
= MoveFileA(source
, dest
);
862 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
863 "MoveFileA: unexpected error %d\n", GetLastError());
865 ret
= DeleteFileA(dest
);
866 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
868 ret
= MoveFileA(source
, dest
);
869 ok(ret
, "MoveFileA: failed, error %d\n", GetLastError());
871 lstrcatA(tempdir
, "Remove Me");
872 ret
= CreateDirectoryA(tempdir
, NULL
);
873 ok(ret
== TRUE
, "CreateDirectoryA failed\n");
875 lstrcpyA(source
, dest
);
876 lstrcpyA(dest
, tempdir
);
877 lstrcatA(dest
, "\\wild?.*");
878 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
879 ret
= MoveFileA(source
, dest
);
880 ok(!ret
, "MoveFileA: shouldn't move to wildcard file\n");
881 ok(GetLastError() == ERROR_INVALID_NAME
|| /* NT */
882 GetLastError() == ERROR_FILE_NOT_FOUND
, /* Win9x */
883 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
884 if (ret
|| (GetLastError() != ERROR_INVALID_NAME
))
887 char temppath
[MAX_PATH
];
890 lstrcpyA(temppath
, tempdir
);
891 lstrcatA(temppath
, "\\*.*");
892 hFind
= FindFirstFileA(temppath
, &fd
);
893 if (INVALID_HANDLE_VALUE
!= hFind
)
898 lpName
= fd
.cAlternateFileName
;
900 lpName
= fd
.cFileName
;
901 ok(IsDotDir(lpName
), "MoveFileA: wildcards file created!\n");
903 while (FindNextFileA(hFind
, &fd
));
907 ret
= DeleteFileA(source
);
908 ok(ret
, "DeleteFileA: error %d\n", GetLastError());
909 ret
= DeleteFileA(dest
);
910 ok(!ret
, "DeleteFileA: error %d\n", GetLastError());
911 ret
= RemoveDirectoryA(tempdir
);
912 ok(ret
, "DeleteDirectoryA: error %d\n", GetLastError());
915 static void test_MoveFileW(void)
917 WCHAR temp_path
[MAX_PATH
];
918 WCHAR source
[MAX_PATH
], dest
[MAX_PATH
];
919 static const WCHAR prefix
[] = {'p','f','x',0};
922 ret
= GetTempPathW(MAX_PATH
, temp_path
);
923 if (ret
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
925 ok(ret
!= 0, "GetTempPathW error %d\n", GetLastError());
926 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
928 ret
= GetTempFileNameW(temp_path
, prefix
, 0, source
);
929 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
931 ret
= GetTempFileNameW(temp_path
, prefix
, 0, dest
);
932 ok(ret
!= 0, "GetTempFileNameW error %d\n", GetLastError());
934 ret
= MoveFileW(source
, dest
);
935 ok(!ret
&& GetLastError() == ERROR_ALREADY_EXISTS
,
936 "CopyFileW: unexpected error %d\n", GetLastError());
938 ret
= DeleteFileW(source
);
939 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
940 ret
= DeleteFileW(dest
);
941 ok(ret
, "DeleteFileW: error %d\n", GetLastError());
944 #define PATTERN_OFFSET 0x10
946 static void test_offset_in_overlapped_structure(void)
952 BYTE buf
[256], pattern
[] = "TeSt";
954 char temp_path
[MAX_PATH
], temp_fname
[MAX_PATH
];
957 ret
=GetTempPathA(MAX_PATH
, temp_path
);
958 ok( ret
, "GetTempPathA error %d\n", GetLastError());
959 ret
=GetTempFileNameA(temp_path
, "pfx", 0, temp_fname
);
960 ok( ret
, "GetTempFileNameA error %d\n", GetLastError());
962 /*** Write File *****************************************************/
964 hFile
= CreateFileA(temp_fname
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
965 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
967 for(i
= 0; i
< sizeof(buf
); i
++) buf
[i
] = i
;
968 ret
= WriteFile(hFile
, buf
, sizeof(buf
), &done
, NULL
);
969 ok( ret
, "WriteFile error %d\n", GetLastError());
970 ok(done
== sizeof(buf
), "expected number of bytes written %u\n", done
);
972 memset(&ov
, 0, sizeof(ov
));
973 S(U(ov
)).Offset
= PATTERN_OFFSET
;
974 S(U(ov
)).OffsetHigh
= 0;
975 rc
=WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
976 /* Win 9x does not support the overlapped I/O on files */
977 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
978 ok(rc
, "WriteFile error %d\n", GetLastError());
979 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
980 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
981 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
983 S(U(ov
)).Offset
= sizeof(buf
) * 2;
984 S(U(ov
)).OffsetHigh
= 0;
985 ret
= WriteFile(hFile
, pattern
, sizeof(pattern
), &done
, &ov
);
986 ok( ret
, "WriteFile error %d\n", GetLastError());
987 ok(done
== sizeof(pattern
), "expected number of bytes written %u\n", done
);
988 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
989 ok(offset
== sizeof(buf
) * 2 + sizeof(pattern
), "wrong file offset %d\n", offset
);
994 /*** Read File *****************************************************/
996 hFile
= CreateFileA(temp_fname
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
997 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError());
999 memset(buf
, 0, sizeof(buf
));
1000 memset(&ov
, 0, sizeof(ov
));
1001 S(U(ov
)).Offset
= PATTERN_OFFSET
;
1002 S(U(ov
)).OffsetHigh
= 0;
1003 rc
=ReadFile(hFile
, buf
, sizeof(pattern
), &done
, &ov
);
1004 /* Win 9x does not support the overlapped I/O on files */
1005 if (rc
|| GetLastError()!=ERROR_INVALID_PARAMETER
) {
1006 ok(rc
, "ReadFile error %d\n", GetLastError());
1007 ok(done
== sizeof(pattern
), "expected number of bytes read %u\n", done
);
1008 offset
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
1009 ok(offset
== PATTERN_OFFSET
+ sizeof(pattern
), "wrong file offset %d\n", offset
);
1010 ok(!memcmp(buf
, pattern
, sizeof(pattern
)), "pattern match failed\n");
1015 ret
= DeleteFileA(temp_fname
);
1016 ok( ret
, "DeleteFileA error %d\n", GetLastError());
1019 static void test_LockFile(void)
1023 OVERLAPPED overlapped
;
1024 int limited_LockFile
;
1025 int limited_UnLockFile
;
1027 handle
= CreateFileA( filename
, GENERIC_READ
| GENERIC_WRITE
,
1028 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1029 CREATE_ALWAYS
, 0, 0 );
1030 if (handle
== INVALID_HANDLE_VALUE
)
1032 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1035 ok( WriteFile( handle
, sillytext
, strlen(sillytext
), &written
, NULL
), "write failed\n" );
1037 ok( LockFile( handle
, 0, 0, 0, 0 ), "LockFile failed\n" );
1038 ok( UnlockFile( handle
, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1040 limited_UnLockFile
= 0;
1041 if (UnlockFile( handle
, 0, 0, 0, 0 ))
1043 limited_UnLockFile
= 1;
1046 ok( LockFile( handle
, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1047 /* overlapping locks must fail */
1048 ok( !LockFile( handle
, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1049 ok( !LockFile( handle
, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1050 /* non-overlapping locks must succeed */
1051 ok( LockFile( handle
, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1053 ok( !UnlockFile( handle
, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1054 ok( UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1055 ok( !UnlockFile( handle
, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1056 ok( UnlockFile( handle
, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1058 S(U(overlapped
)).Offset
= 100;
1059 S(U(overlapped
)).OffsetHigh
= 0;
1060 overlapped
.hEvent
= 0;
1062 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1063 if (LockFileEx( handle
, 0, 0, 100, 0, &overlapped
))
1065 /* LockFileEx is probably OK, test it more. */
1066 ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
),
1067 "LockFileEx 100,100 failed\n" );
1070 /* overlapping shared locks are OK */
1071 S(U(overlapped
)).Offset
= 150;
1072 limited_UnLockFile
|| ok( LockFileEx( handle
, 0, 0, 100, 0, &overlapped
), "LockFileEx 150,100 failed\n" );
1074 /* but exclusive is not */
1075 ok( !LockFileEx( handle
, LOCKFILE_EXCLUSIVE_LOCK
|LOCKFILE_FAIL_IMMEDIATELY
,
1076 0, 50, 0, &overlapped
),
1077 "LockFileEx exclusive 150,50 succeeded\n" );
1078 if (!UnlockFileEx( handle
, 0, 100, 0, &overlapped
))
1079 { /* UnLockFile is capable. */
1080 S(U(overlapped
)).Offset
= 100;
1081 ok( !UnlockFileEx( handle
, 0, 100, 0, &overlapped
),
1082 "UnlockFileEx 150,100 again succeeded\n" );
1085 ok( LockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1086 ok( !LockFile( handle
, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1087 ok( !LockFile( handle
, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1088 ok( UnlockFile( handle
, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1090 /* wrap-around lock should not do anything */
1091 /* (but still succeeds on NT4 so we don't check result) */
1092 LockFile( handle
, 0, 0x10000000, 0, 0xf0000001 );
1094 limited_LockFile
= 0;
1095 if (!LockFile( handle
, ~0, ~0, 1, 0 ))
1097 limited_LockFile
= 1;
1100 limited_UnLockFile
|| ok( UnlockFile( handle
, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1102 /* zero-byte lock */
1103 ok( LockFile( handle
, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1104 limited_LockFile
|| ok( !LockFile( handle
, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1105 ok( LockFile( handle
, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1106 limited_LockFile
|| ok( !LockFile( handle
, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1108 ok( UnlockFile( handle
, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1109 !ok( UnlockFile( handle
, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1111 ok( UnlockFile( handle
, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1113 CloseHandle( handle
);
1114 DeleteFileA( filename
);
1117 static inline int is_sharing_compatible( DWORD access1
, DWORD sharing1
, DWORD access2
, DWORD sharing2
, BOOL is_win9x
)
1121 if (!access1
) sharing1
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1122 if (!access2
) sharing2
= FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
;
1127 if (!access1
) access1
= GENERIC_READ
;
1130 if (!access2
) access2
= GENERIC_READ
;
1133 if ((access1
& GENERIC_READ
) && !(sharing2
& FILE_SHARE_READ
)) return 0;
1134 if ((access1
& GENERIC_WRITE
) && !(sharing2
& FILE_SHARE_WRITE
)) return 0;
1135 if ((access1
& DELETE
) && !(sharing2
& FILE_SHARE_DELETE
)) return 0;
1136 if ((access2
& GENERIC_READ
) && !(sharing1
& FILE_SHARE_READ
)) return 0;
1137 if ((access2
& GENERIC_WRITE
) && !(sharing1
& FILE_SHARE_WRITE
)) return 0;
1138 if ((access2
& DELETE
) && !(sharing1
& FILE_SHARE_DELETE
)) return 0;
1142 static void test_file_sharing(void)
1144 static const DWORD access_modes
[] =
1145 { 0, GENERIC_READ
, GENERIC_WRITE
, GENERIC_READ
|GENERIC_WRITE
,
1146 DELETE
, GENERIC_READ
|DELETE
, GENERIC_WRITE
|DELETE
, GENERIC_READ
|GENERIC_WRITE
|DELETE
};
1147 static const DWORD sharing_modes
[] =
1148 { 0, FILE_SHARE_READ
,
1149 FILE_SHARE_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1150 FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_DELETE
,
1151 FILE_SHARE_WRITE
|FILE_SHARE_DELETE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
};
1155 BOOL is_win9x
= FALSE
;
1157 /* make sure the file exists */
1158 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1159 if (h
== INVALID_HANDLE_VALUE
)
1161 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename
, GetLastError());
1164 is_win9x
= GetFileAttributesW(filenameW
) == INVALID_FILE_ATTRIBUTES
;
1167 for (a1
= 0; a1
< sizeof(access_modes
)/sizeof(access_modes
[0]); a1
++)
1169 for (s1
= 0; s1
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s1
++)
1171 /* Win9x doesn't support FILE_SHARE_DELETE */
1172 if (is_win9x
&& (sharing_modes
[s1
] & FILE_SHARE_DELETE
))
1175 SetLastError(0xdeadbeef);
1176 h
= CreateFileA( filename
, access_modes
[a1
], sharing_modes
[s1
],
1177 NULL
, OPEN_EXISTING
, 0, 0 );
1178 if (h
== INVALID_HANDLE_VALUE
)
1180 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename
,GetLastError());
1183 for (a2
= 0; a2
< sizeof(access_modes
)/sizeof(access_modes
[0]); a2
++)
1185 for (s2
= 0; s2
< sizeof(sharing_modes
)/sizeof(sharing_modes
[0]); s2
++)
1187 /* Win9x doesn't support FILE_SHARE_DELETE */
1188 if (is_win9x
&& (sharing_modes
[s2
] & FILE_SHARE_DELETE
))
1191 SetLastError(0xdeadbeef);
1192 h2
= CreateFileA( filename
, access_modes
[a2
], sharing_modes
[s2
],
1193 NULL
, OPEN_EXISTING
, 0, 0 );
1195 if (is_sharing_compatible( access_modes
[a1
], sharing_modes
[s1
],
1196 access_modes
[a2
], sharing_modes
[s2
], is_win9x
))
1198 ret
= GetLastError();
1200 ok( h2
!= INVALID_HANDLE_VALUE
,
1201 "open failed for modes %x/%x/%x/%x\n",
1202 access_modes
[a1
], sharing_modes
[s1
],
1203 access_modes
[a2
], sharing_modes
[s2
] );
1204 ok( ret
== 0xdeadbeef /* Win9x */ ||
1206 "wrong error code %d\n", ret
);
1212 ret
= GetLastError();
1214 ok( h2
== INVALID_HANDLE_VALUE
,
1215 "open succeeded for modes %x/%x/%x/%x\n",
1216 access_modes
[a1
], sharing_modes
[s1
],
1217 access_modes
[a2
], sharing_modes
[s2
] );
1218 ok( ret
== ERROR_SHARING_VIOLATION
,
1219 "wrong error code %d\n", ret
);
1227 SetLastError(0xdeadbeef);
1228 h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, OPEN_ALWAYS
, 0, 0 );
1229 ok( h
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1231 SetLastError(0xdeadbeef);
1232 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
1233 ok( h2
== INVALID_HANDLE_VALUE
, "CreateFileA should fail\n");
1234 ok( GetLastError() == ERROR_SHARING_VIOLATION
, "wrong error code %d\n", GetLastError() );
1236 h2
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, 0 );
1237 ok( h2
!= INVALID_HANDLE_VALUE
, "CreateFileA error %d\n", GetLastError() );
1242 DeleteFileA( filename
);
1245 static char get_windows_drive(void)
1247 char windowsdir
[MAX_PATH
];
1248 GetWindowsDirectory(windowsdir
, sizeof(windowsdir
));
1249 return windowsdir
[0];
1252 static void test_FindFirstFileA(void)
1255 WIN32_FIND_DATAA data
;
1257 char buffer
[5] = "C:\\";
1260 /* try FindFirstFileA on "C:\" */
1261 buffer
[0] = get_windows_drive();
1263 SetLastError( 0xdeadbeaf );
1264 handle
= FindFirstFileA(buffer
, &data
);
1265 err
= GetLastError();
1266 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on root directory should fail\n" );
1267 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1269 /* try FindFirstFileA on "C:\*" */
1270 strcpy(buffer2
, buffer
);
1271 strcat(buffer2
, "*");
1272 handle
= FindFirstFileA(buffer2
, &data
);
1273 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1274 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1275 "FindFirstFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1276 if (FindNextFileA( handle
, &data
))
1277 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1278 "FindNextFile shouldn't return '%s' in drive root\n", data
.cFileName
);
1279 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1281 /* try FindFirstFileA on windows dir */
1282 GetWindowsDirectory( buffer2
, sizeof(buffer2
) );
1283 strcat(buffer2
, "\\*");
1284 handle
= FindFirstFileA(buffer2
, &data
);
1285 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s should succeed\n", buffer2
);
1286 ok( !strcmp( data
.cFileName
, "." ), "FindFirstFile should return '.' first\n" );
1287 ok( FindNextFileA( handle
, &data
), "FindNextFile failed\n" );
1288 ok( !strcmp( data
.cFileName
, ".." ), "FindNextFile should return '..' as second entry\n" );
1289 while (FindNextFileA( handle
, &data
))
1290 ok ( strcmp( data
.cFileName
, "." ) && strcmp( data
.cFileName
, ".." ),
1291 "FindNextFile shouldn't return '%s'\n", data
.cFileName
);
1292 ok ( FindClose(handle
) == TRUE
, "Failed to close handle %s\n", buffer2
);
1294 /* try FindFirstFileA on "C:\foo\" */
1295 SetLastError( 0xdeadbeaf );
1296 strcpy(buffer2
, buffer
);
1297 strcat(buffer2
, "foo\\");
1298 handle
= FindFirstFileA(buffer2
, &data
);
1299 err
= GetLastError();
1300 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1302 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1305 /* try FindFirstFileA on "C:\foo\bar.txt" */
1306 SetLastError( 0xdeadbeaf );
1307 strcpy(buffer2
, buffer
);
1308 strcat(buffer2
, "foo\\bar.txt");
1309 handle
= FindFirstFileA(buffer2
, &data
);
1310 err
= GetLastError();
1311 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1312 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1314 /* try FindFirstFileA on "C:\foo\*.*" */
1315 SetLastError( 0xdeadbeaf );
1316 strcpy(buffer2
, buffer
);
1317 strcat(buffer2
, "foo\\*.*");
1318 handle
= FindFirstFileA(buffer2
, &data
);
1319 err
= GetLastError();
1320 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1321 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1323 /* try FindFirstFileA on "foo\bar.txt" */
1324 SetLastError( 0xdeadbeaf );
1325 strcpy(buffer2
, "foo\\bar.txt");
1326 handle
= FindFirstFileA(buffer2
, &data
);
1327 err
= GetLastError();
1328 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1329 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1331 /* try FindFirstFileA on "c:\nul" */
1332 SetLastError( 0xdeadbeaf );
1333 strcpy(buffer2
, buffer
);
1334 strcat(buffer2
, "nul");
1335 handle
= FindFirstFileA(buffer2
, &data
);
1336 err
= GetLastError();
1337 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1338 ok( 0 == lstrcmpiA(data
.cFileName
, "nul"), "wrong name %s\n", data
.cFileName
);
1339 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1340 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1341 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
, "wrong attributes %x\n", data
.dwFileAttributes
);
1342 SetLastError( 0xdeadbeaf );
1343 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1344 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1345 ok( FindClose( handle
), "failed to close handle\n" );
1347 /* try FindFirstFileA on "lpt1" */
1348 SetLastError( 0xdeadbeaf );
1349 strcpy(buffer2
, "lpt1");
1350 handle
= FindFirstFileA(buffer2
, &data
);
1351 err
= GetLastError();
1352 ok( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on %s failed\n", buffer2
);
1353 ok( 0 == lstrcmpiA(data
.cFileName
, "lpt1"), "wrong name %s\n", data
.cFileName
);
1354 ok( 0 == data
.nFileSizeHigh
, "wrong size %d\n", data
.nFileSizeHigh
);
1355 ok( 0 == data
.nFileSizeLow
, "wrong size %d\n", data
.nFileSizeLow
);
1356 ok( FILE_ATTRIBUTE_ARCHIVE
== data
.dwFileAttributes
, "wrong attributes %x\n", data
.dwFileAttributes
);
1357 SetLastError( 0xdeadbeaf );
1358 ok( !FindNextFileA( handle
, &data
), "FindNextFileA succeeded\n" );
1359 ok( GetLastError() == ERROR_NO_MORE_FILES
, "bad error %d\n", GetLastError() );
1360 ok( FindClose( handle
), "failed to close handle\n" );
1362 /* try FindFirstFileA on "c:\nul\*" */
1363 SetLastError( 0xdeadbeaf );
1364 strcpy(buffer2
, buffer
);
1365 strcat(buffer2
, "nul\\*");
1366 handle
= FindFirstFileA(buffer2
, &data
);
1367 err
= GetLastError();
1368 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1369 ok ( err
== ERROR_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1371 /* try FindFirstFileA on "c:\nul*" */
1372 SetLastError( 0xdeadbeaf );
1373 strcpy(buffer2
, buffer
);
1374 strcat(buffer2
, "nul*");
1375 handle
= FindFirstFileA(buffer2
, &data
);
1376 err
= GetLastError();
1377 ok ( handle
== INVALID_HANDLE_VALUE
, "FindFirstFile on %s should Fail\n", buffer2
);
1378 ok ( err
== ERROR_FILE_NOT_FOUND
, "Bad Error number %d\n", err
);
1380 /* try FindFirstFileA on "c:\foo\bar\nul" */
1381 SetLastError( 0xdeadbeaf );
1382 strcpy(buffer2
, buffer
);
1383 strcat(buffer2
, "foo\\bar\\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:\foo\nul\bar" */
1390 SetLastError( 0xdeadbeaf );
1391 strcpy(buffer2
, buffer
);
1392 strcat(buffer2
, "foo\\nul\\bar");
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_PATH_NOT_FOUND
, "Bad Error number %d\n", err
);
1399 static void test_FindNextFileA(void)
1402 WIN32_FIND_DATAA search_results
;
1404 char buffer
[5] = "C:\\*";
1406 buffer
[0] = get_windows_drive();
1407 handle
= FindFirstFileA(buffer
,&search_results
);
1408 ok ( handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile on C:\\* should succeed\n" );
1409 while (FindNextFile(handle
, &search_results
))
1411 /* get to the end of the files */
1413 ok ( FindClose(handle
) == TRUE
, "Failed to close handle\n");
1414 err
= GetLastError();
1415 ok ( err
== ERROR_NO_MORE_FILES
, "GetLastError should return ERROR_NO_MORE_FILES\n");
1418 static void test_FindFirstFileExA(void)
1420 WIN32_FIND_DATAA search_results
;
1423 CreateDirectoryA("test-dir", NULL
);
1424 _lclose(_lcreat("test-dir\\file1", 0));
1425 _lclose(_lcreat("test-dir\\file2", 0));
1426 CreateDirectoryA("test-dir\\dir1", NULL
);
1427 /* FindExLimitToDirectories is ignored */
1428 handle
= FindFirstFileExA("test-dir\\*", FindExInfoStandard
, &search_results
, FindExSearchLimitToDirectories
, NULL
, 0);
1429 ok(handle
!= INVALID_HANDLE_VALUE
, "FindFirstFile failed (err=%u)\n", GetLastError());
1430 ok(strcmp(search_results
.cFileName
, ".") == 0, "First entry should be '.', is %s\n", search_results
.cFileName
);
1432 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1434 ok(FindNextFile(handle
, &search_results
), "Fetching second file failed\n");
1435 ok(strcmp(search_results
.cFileName
, "..") == 0, "Second entry should be '..' is %s\n", search_results
.cFileName
);
1437 ok(FindNextFile(handle
, &search_results
), "Fetching third file failed\n");
1438 ok(CHECK_NAME(search_results
.cFileName
), "Invalid thrid entry - %s\n", search_results
.cFileName
);
1440 ok(FindNextFile(handle
, &search_results
), "Fetching fourth file failed\n");
1441 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fourth entry - %s\n", search_results
.cFileName
);
1443 ok(FindNextFile(handle
, &search_results
), "Fetching fifth file failed\n");
1444 ok(CHECK_NAME(search_results
.cFileName
), "Invalid fifth entry - %s\n", search_results
.cFileName
);
1448 ok(FindNextFile(handle
, &search_results
) == FALSE
, "Fetching sixth file should failed\n");
1449 DeleteFileA("test-dir\\file1");
1450 DeleteFileA("test-dir\\file2");
1451 RemoveDirectoryA("test-dir\\dir1");
1452 RemoveDirectoryA("test-dir");
1455 static int test_Mapfile_createtemp(HANDLE
*handle
)
1457 SetFileAttributesA(filename
,FILE_ATTRIBUTE_NORMAL
);
1458 DeleteFile(filename
);
1459 *handle
= CreateFile(filename
, GENERIC_READ
|GENERIC_WRITE
, 0, 0,
1460 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1461 if (*handle
!= INVALID_HANDLE_VALUE
) {
1469 static void test_MapFile(void)
1474 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1476 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0x1000, "named_file_map" );
1477 ok( hmap
!= NULL
, "mapping should work, I named it!\n" );
1479 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1481 /* We have to close file before we try new stuff with mapping again.
1482 Else we would always succeed on XP or block descriptors on 95. */
1483 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1484 ok( hmap
!= NULL
, "We should still be able to map!\n" );
1485 ok( CloseHandle( hmap
), "can't close mapping handle\n");
1486 ok( CloseHandle( handle
), "can't close file handle\n");
1489 ok(test_Mapfile_createtemp(&handle
), "Couldn't create test file.\n");
1491 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0, 0, NULL
);
1492 ok( hmap
== NULL
, "mapped zero size file\n");
1493 ok( GetLastError() == ERROR_FILE_INVALID
, "not ERROR_FILE_INVALID\n");
1495 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0, NULL
);
1496 ok( hmap
== NULL
, "mapping should fail\n");
1497 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1499 hmap
= CreateFileMapping( handle
, NULL
, PAGE_READWRITE
, 0x80000000, 0x10000, NULL
);
1500 ok( hmap
== NULL
, "mapping should fail\n");
1501 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1503 /* On XP you can now map again, on Win 95 you cannot. */
1505 ok( CloseHandle( handle
), "can't close file handle\n");
1506 ok( DeleteFileA( filename
), "DeleteFile failed after map\n" );
1509 static void test_GetFileType(void)
1512 HANDLE h
= CreateFileA( filename
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
1513 ok( h
!= INVALID_HANDLE_VALUE
, "open %s failed\n", filename
);
1514 type
= GetFileType(h
);
1515 ok( type
== FILE_TYPE_DISK
, "expected type disk got %d\n", type
);
1517 h
= CreateFileA( "nul", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0 );
1518 ok( h
!= INVALID_HANDLE_VALUE
, "open nul failed\n" );
1519 type
= GetFileType(h
);
1520 ok( type
== FILE_TYPE_CHAR
, "expected type char for nul got %d\n", type
);
1522 DeleteFileA( filename
);
1525 static int completion_count
;
1527 static void CALLBACK
FileIOComplete(DWORD dwError
, DWORD dwBytes
, LPOVERLAPPED ovl
)
1529 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1530 ReleaseSemaphore(ovl
->hEvent
, 1, NULL
);
1534 static void test_async_file_errors(void)
1536 char szFile
[MAX_PATH
];
1537 HANDLE hSem
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1539 LPVOID lpBuffer
= HeapAlloc(GetProcessHeap(), 0, 4096);
1541 S(U(ovl
)).Offset
= 0;
1542 S(U(ovl
)).OffsetHigh
= 0;
1544 completion_count
= 0;
1546 GetWindowsDirectoryA(szFile
, sizeof(szFile
)/sizeof(szFile
[0])-1-strlen("\\win.ini"));
1547 strcat(szFile
, "\\win.ini");
1548 hFile
= CreateFileA(szFile
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
, OPEN_ALWAYS
, FILE_FLAG_OVERLAPPED
, NULL
);
1549 ok(hFile
!= NULL
, "CreateFileA(%s ...) failed\n", szFile
);
1553 while (WaitForSingleObjectEx(hSem
, INFINITE
, TRUE
) == WAIT_IO_COMPLETION
)
1555 res
= ReadFileEx(hFile
, lpBuffer
, 4096, &ovl
, FileIOComplete
);
1556 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1559 S(U(ovl
)).Offset
+= 4096;
1560 /* i/o completion routine only called if ReadFileEx returned success.
1561 * we only care about violations of this rule so undo what should have
1565 ok(completion_count
== 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count
);
1566 /*printf("Error = %ld\n", GetLastError());*/
1569 static void test_read_write(void)
1573 char temp_path
[MAX_PATH
];
1574 char filename
[MAX_PATH
];
1575 static const char prefix
[] = "pfx";
1577 ret
= GetTempPathA(MAX_PATH
, temp_path
);
1578 ok(ret
!= 0, "GetTempPathA error %d\n", GetLastError());
1579 ok(ret
< MAX_PATH
, "temp path should fit into MAX_PATH\n");
1581 ret
= GetTempFileNameA(temp_path
, prefix
, 0, filename
);
1582 ok(ret
!= 0, "GetTempFileNameA error %d\n", GetLastError());
1584 hFile
= CreateFileA(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1585 CREATE_ALWAYS
, FILE_FLAG_RANDOM_ACCESS
, 0);
1586 ok(hFile
!= INVALID_HANDLE_VALUE
, "CreateFileA: error %d\n", GetLastError());
1588 SetLastError(12345678);
1590 ret
= WriteFile(hFile
, NULL
, 0, &bytes
, NULL
);
1591 ok(ret
&& GetLastError() == 12345678,
1592 "ret = %d, error %d\n", ret
, GetLastError());
1593 ok(!bytes
, "bytes = %d\n", bytes
);
1595 SetLastError(12345678);
1597 ret
= WriteFile(hFile
, NULL
, 10, &bytes
, NULL
);
1598 ok((!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
) || /* Win2k */
1599 (ret
&& GetLastError() == 12345678), /* Win9x */
1600 "ret = %d, error %d\n", ret
, GetLastError());
1601 ok(!bytes
|| /* Win2k */
1602 bytes
== 10, /* Win9x */
1603 "bytes = %d\n", bytes
);
1605 /* make sure the file contains data */
1606 WriteFile(hFile
, "this is the test data", 21, &bytes
, NULL
);
1607 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
1609 SetLastError(12345678);
1611 ret
= ReadFile(hFile
, NULL
, 0, &bytes
, NULL
);
1612 ok(ret
&& GetLastError() == 12345678,
1613 "ret = %d, error %d\n", ret
, GetLastError());
1614 ok(!bytes
, "bytes = %d\n", bytes
);
1616 SetLastError(12345678);
1618 ret
= ReadFile(hFile
, NULL
, 10, &bytes
, NULL
);
1619 ok(!ret
&& (GetLastError() == ERROR_NOACCESS
|| /* Win2k */
1620 GetLastError() == ERROR_INVALID_PARAMETER
), /* Win9x */
1621 "ret = %d, error %d\n", ret
, GetLastError());
1622 ok(!bytes
, "bytes = %d\n", bytes
);
1624 ret
= CloseHandle(hFile
);
1625 ok( ret
, "CloseHandle: error %d\n", GetLastError());
1626 ret
= DeleteFileA(filename
);
1627 ok( ret
, "DeleteFileA: error %d\n", GetLastError());
1630 static void test_OpenFile(void)
1637 static const char *file
= "\\regsvr32.exe";
1638 static const char *foo
= ".\\foo-bar-foo.baz";
1639 static const char *foo_too_long
= ".\\foo-bar-foo.baz+++++++++++++++"
1640 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1641 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1642 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1643 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1644 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1645 static const char *backslash
= "\\";
1646 char buff
[MAX_PATH
];
1647 char buff_long
[4*MAX_PATH
];
1648 char filled_0xA5
[OFS_MAXPATHNAME
];
1651 /* Check for existing file */
1652 length
= GetSystemDirectoryA(buff
, MAX_PATH
);
1654 if (length
+ lstrlen(file
) < MAX_PATH
)
1656 lstrcatA(buff
, file
);
1657 memset(&ofs
, 0xA5, sizeof(ofs
));
1658 SetLastError(0xfaceabee);
1660 hFile
= OpenFile(buff
, &ofs
, OF_EXIST
);
1661 ok( hFile
== TRUE
, "%s not found : %d\n", buff
, GetLastError() );
1662 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1663 "GetLastError() returns %d\n", GetLastError() );
1664 ok( ofs
.cBytes
== sizeof(ofs
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1665 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1666 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1667 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1668 ofs
.szPathName
, buff
);
1671 memset(&filled_0xA5
, 0xA5, OFS_MAXPATHNAME
);
1672 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1674 /* Check for nonexistent file */
1675 if (length
+ lstrlenA(foo
+ 1) < MAX_PATH
)
1677 lstrcatA(buff
, foo
+ 1); /* Avoid '.' during concatenation */
1678 memset(&ofs
, 0xA5, sizeof(ofs
));
1679 SetLastError(0xfaceabee);
1681 hFile
= OpenFile(foo
, &ofs
, OF_EXIST
);
1682 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1683 ok( GetLastError() == ERROR_FILE_NOT_FOUND
, "GetLastError() returns %d\n", GetLastError() );
1685 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1686 ok( ofs
.nErrCode
== ERROR_FILE_NOT_FOUND
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1687 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0 || strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1688 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1689 ofs
.szPathName
, buff
);
1692 length
= GetCurrentDirectoryA(MAX_PATH
, buff_long
);
1693 length
+= lstrlenA(foo_too_long
+ 1);
1695 /* Check for nonexistent file with too long filename */
1696 if (length
>= OFS_MAXPATHNAME
&& length
< sizeof(buff_long
))
1698 lstrcatA(buff_long
, foo_too_long
+ 1); /* Avoid '.' during concatenation */
1699 memset(&ofs
, 0xA5, sizeof(ofs
));
1700 SetLastError(0xfaceabee);
1702 hFile
= OpenFile(foo_too_long
, &ofs
, OF_EXIST
);
1703 ok( hFile
== HFILE_ERROR
, "hFile != HFILE_ERROR : %d\n", GetLastError());
1704 ok( GetLastError() == ERROR_INVALID_DATA
|| GetLastError() == ERROR_FILENAME_EXCED_RANGE
,
1705 "GetLastError() returns %d\n", GetLastError() );
1707 ok( ofs
.cBytes
== 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1708 ok( ofs
.nErrCode
== ERROR_INVALID_DATA
|| ofs
.nErrCode
== ERROR_FILENAME_EXCED_RANGE
,
1709 "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1710 ok( strncmp(ofs
.szPathName
, filled_0xA5
, OFS_MAXPATHNAME
) == 0,
1711 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1715 length
= GetCurrentDirectoryA(MAX_PATH
, buff
);
1716 length
+= lstrlenA(backslash
);
1717 length
+= lstrlenA(filename
);
1719 if (length
>= MAX_PATH
)
1721 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length
, MAX_PATH
);
1724 lstrcatA(buff
, backslash
);
1725 lstrcatA(buff
, filename
);
1727 memset(&ofs
, 0xA5, sizeof(ofs
));
1728 SetLastError(0xfaceabee);
1729 /* Create an empty file */
1730 hFile
= OpenFile(filename
, &ofs
, OF_CREATE
);
1731 ok( hFile
!= HFILE_ERROR
, "OpenFile failed to create nonexistent file\n" );
1732 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1733 "GetLastError() returns %d\n", GetLastError() );
1734 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1735 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1736 ret
= CloseHandle((HANDLE
)hFile
);
1737 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1738 retval
= GetFileAttributesA(filename
);
1739 ok( retval
!= INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA: error %d\n", GetLastError() );
1741 memset(&ofs
, 0xA5, sizeof(ofs
));
1742 SetLastError(0xfaceabee);
1743 /* Check various opening options: */
1744 /* for reading only, */
1745 hFile
= OpenFile(filename
, &ofs
, OF_READ
);
1746 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read\n" );
1747 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1748 "GetLastError() returns %d\n", GetLastError() );
1749 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1750 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1751 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1752 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1753 ret
= CloseHandle((HANDLE
)hFile
);
1754 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1756 memset(&ofs
, 0xA5, sizeof(ofs
));
1757 SetLastError(0xfaceabee);
1758 /* for writing only, */
1759 hFile
= OpenFile(filename
, &ofs
, OF_WRITE
);
1760 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on write\n" );
1761 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1762 "GetLastError() returns %d\n", GetLastError() );
1763 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1764 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1765 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1766 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1767 ret
= CloseHandle((HANDLE
)hFile
);
1768 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1770 memset(&ofs
, 0xA5, sizeof(ofs
));
1771 SetLastError(0xfaceabee);
1772 /* for reading and writing, */
1773 hFile
= OpenFile(filename
, &ofs
, OF_READWRITE
);
1774 ok( hFile
!= HFILE_ERROR
, "OpenFile failed on read/write\n" );
1775 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1776 "GetLastError() returns %d\n", GetLastError() );
1777 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1778 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1779 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1780 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1781 ret
= CloseHandle((HANDLE
)hFile
);
1782 ok( ret
== TRUE
, "CloseHandle() returns %d\n", ret
);
1784 memset(&ofs
, 0xA5, sizeof(ofs
));
1785 SetLastError(0xfaceabee);
1786 /* for checking file presence. */
1787 hFile
= OpenFile(filename
, &ofs
, OF_EXIST
);
1788 ok( hFile
== 1, "OpenFile failed on finding our created file\n" );
1789 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1790 "GetLastError() returns %d\n", GetLastError() );
1791 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1792 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1793 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1794 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1796 memset(&ofs
, 0xA5, sizeof(ofs
));
1797 SetLastError(0xfaceabee);
1798 /* Delete the file and make sure it doesn't exist anymore */
1799 hFile
= OpenFile(filename
, &ofs
, OF_DELETE
);
1800 ok( hFile
== 1, "OpenFile failed on delete (%d)\n", hFile
);
1801 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS
,
1802 "GetLastError() returns %d\n", GetLastError() );
1803 ok( ofs
.cBytes
== sizeof(OFSTRUCT
), "OpenFile set ofs.cBytes to %d\n", ofs
.cBytes
);
1804 ok( ofs
.nErrCode
== ERROR_SUCCESS
, "OpenFile set ofs.nErrCode to %d\n", ofs
.nErrCode
);
1805 ok( lstrcmpiA(ofs
.szPathName
, buff
) == 0,
1806 "OpenFile returned '%s', but was expected to return '%s'\n", ofs
.szPathName
, buff
);
1808 retval
= GetFileAttributesA(filename
);
1809 ok( retval
== INVALID_FILE_ATTRIBUTES
, "GetFileAttributesA succeeded on deleted file\n" );
1812 static void test_overlapped(void)
1817 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1819 memset( &ov
, 0, sizeof ov
);
1821 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1822 ok( r
== TRUE
, "should return false\n");
1823 ok( result
== 0, "wrong result %u\n", result
);
1827 ov
.InternalHigh
= 0xabcd;
1828 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1829 ok( r
== TRUE
, "should return false\n");
1830 ok( result
== 0xabcd, "wrong result %u\n", result
);
1832 SetLastError( 0xb00 );
1834 ov
.Internal
= STATUS_INVALID_HANDLE
;
1835 ov
.InternalHigh
= 0xabcd;
1836 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1837 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
1838 ok( r
== FALSE
, "should return false\n");
1839 ok( result
== 0xabcd, "wrong result %u\n", result
);
1841 SetLastError( 0xb00 );
1843 ov
.Internal
= STATUS_PENDING
;
1844 ov
.InternalHigh
= 0xabcd;
1845 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1846 ok( GetLastError() == ERROR_IO_INCOMPLETE
, "wrong error %u\n", GetLastError() );
1847 ok( r
== FALSE
, "should return false\n");
1848 ok( result
== 0, "wrong result %u\n", result
);
1850 SetLastError( 0xb00 );
1851 ov
.hEvent
= CreateEvent( NULL
, 1, 1, NULL
);
1852 ov
.Internal
= STATUS_PENDING
;
1853 ov
.InternalHigh
= 0xabcd;
1854 r
= GetOverlappedResult(0, &ov
, &result
, 0);
1855 ok( GetLastError() == ERROR_IO_INCOMPLETE
, "wrong error %u\n", GetLastError() );
1856 ok( r
== FALSE
, "should return false\n");
1858 ResetEvent( ov
.hEvent
);
1860 SetLastError( 0xb00 );
1861 ov
.Internal
= STATUS_PENDING
;
1862 ov
.InternalHigh
= 0;
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");
1867 r
= CloseHandle( ov
.hEvent
);
1868 ok( r
== TRUE
, "close handle failed\n");
1871 static void test_RemoveDirectory(void)
1874 char directory
[] = "removeme";
1876 rc
= CreateDirectory(directory
, NULL
);
1877 ok( rc
, "Createdirectory failed, gle=%d\n", GetLastError() );
1879 rc
= SetCurrentDirectory(directory
);
1880 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1882 rc
= RemoveDirectory(".");
1884 ok( !rc
, "RemoveDirectory unexpectedly worked\n" );
1887 rc
= SetCurrentDirectory("..");
1888 ok( rc
, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1890 rc
= RemoveDirectory(directory
);
1892 ok( rc
, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1906 test_GetTempFileNameA();
1915 test_FindFirstFileA();
1916 test_FindNextFileA();
1917 test_FindFirstFileExA();
1919 test_file_sharing();
1920 test_offset_in_overlapped_structure();
1923 test_async_file_errors();
1927 test_RemoveDirectory();