kernel32/tests: Get the test to run on Windows 95.
[wine/hacks.git] / dlls / kernel32 / tests / file.c
blob1fac80793c6eccfc51f27dfb14ecac2fefe1e103
1 /*
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
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <time.h>
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
31 static HINSTANCE hkernel32;
32 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
34 /* keep filename and filenameW the same */
35 static const char filename[] = "testfile.xxx";
36 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
37 static const char sillytext[] =
38 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
50 static void test__hread( void )
52 HFILE filehandle;
53 char buffer[10000];
54 long bytes_read;
55 long bytes_wanted;
56 long i;
57 BOOL ret;
59 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
60 DeleteFileA( filename );
61 filehandle = _lcreat( filename, 0 );
62 if (filehandle == HFILE_ERROR)
64 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
65 return;
68 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
70 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
72 filehandle = _lopen( filename, OF_READ );
74 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
76 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
78 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
80 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
82 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
83 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
84 for (i = 0; i < bytes_wanted; i++)
86 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
90 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
92 ret = DeleteFileA( filename );
93 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
97 static void test__hwrite( void )
99 HFILE filehandle;
100 char buffer[10000];
101 long bytes_read;
102 long bytes_written;
103 long blocks;
104 long i;
105 char *contents;
106 HLOCAL memory_object;
107 char checksum[1];
108 BOOL ret;
110 filehandle = _lcreat( filename, 0 );
111 if (filehandle == HFILE_ERROR)
113 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
114 return;
117 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
119 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
121 filehandle = _lopen( filename, OF_READ );
123 bytes_read = _hread( filehandle, buffer, 1);
125 ok( 0 == bytes_read, "file read size error\n" );
127 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
129 filehandle = _lopen( filename, OF_READWRITE );
131 bytes_written = 0;
132 checksum[0] = '\0';
133 srand( (unsigned)time( NULL ) );
134 for (blocks = 0; blocks < 100; blocks++)
136 for (i = 0; i < (long)sizeof( buffer ); i++)
138 buffer[i] = rand( );
139 checksum[0] = checksum[0] + buffer[i];
141 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
142 bytes_written = bytes_written + sizeof( buffer );
145 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
146 bytes_written++;
148 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
150 memory_object = LocalAlloc( LPTR, bytes_written );
152 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
154 contents = LocalLock( memory_object );
156 filehandle = _lopen( filename, OF_READ );
158 contents = LocalLock( memory_object );
160 ok( NULL != contents, "LocalLock whines\n" );
162 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
164 checksum[0] = '\0';
165 i = 0;
168 checksum[0] = checksum[0] + contents[i];
169 i++;
171 while (i < bytes_written - 1);
173 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
175 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
177 ret = DeleteFileA( filename );
178 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
182 static void test__lclose( void )
184 HFILE filehandle;
185 BOOL ret;
187 filehandle = _lcreat( filename, 0 );
188 if (filehandle == HFILE_ERROR)
190 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
191 return;
194 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
196 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
198 ret = DeleteFileA( filename );
199 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
203 static void test__lcreat( void )
205 HFILE filehandle;
206 char buffer[10000];
207 WIN32_FIND_DATAA search_results;
208 char slashname[] = "testfi/";
209 int err;
210 HANDLE find;
211 BOOL ret;
213 filehandle = _lcreat( filename, 0 );
214 if (filehandle == HFILE_ERROR)
216 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
217 return;
220 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
222 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
224 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
226 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
228 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
230 ret = DeleteFileA(filename);
231 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
233 filehandle = _lcreat( filename, 1 ); /* readonly */
234 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
236 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
238 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
240 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
242 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
244 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
246 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
248 filehandle = _lcreat( filename, 2 );
249 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
251 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
253 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
255 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
257 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
259 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
261 ret = DeleteFileA( filename );
262 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
264 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
265 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
267 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
269 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
271 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
273 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
275 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
277 ret = DeleteFileA( filename );
278 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
280 filehandle=_lcreat (slashname, 0); /* illegal name */
281 if (HFILE_ERROR==filehandle) {
282 err=GetLastError ();
283 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
284 "creating file \"%s\" failed with error %d\n", slashname, err);
285 } else { /* only NT succeeds */
286 _lclose(filehandle);
287 find=FindFirstFileA (slashname, &search_results);
288 if (INVALID_HANDLE_VALUE!=find)
290 ret = FindClose (find);
291 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
292 slashname[strlen(slashname)-1]=0;
293 ok (!strcmp (slashname, search_results.cFileName),
294 "found unexpected name \"%s\"\n", search_results.cFileName);
295 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
296 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
297 search_results.dwFileAttributes);
299 ret = DeleteFileA( slashname );
300 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
303 filehandle=_lcreat (filename, 8); /* illegal attribute */
304 if (HFILE_ERROR==filehandle)
305 ok (0, "couldn't create volume label \"%s\"\n", filename);
306 else {
307 _lclose(filehandle);
308 find=FindFirstFileA (filename, &search_results);
309 if (INVALID_HANDLE_VALUE==find)
310 ok (0, "file \"%s\" not found\n", filename);
311 else {
312 ret = FindClose(find);
313 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
314 ok (!strcmp (filename, search_results.cFileName),
315 "found unexpected name \"%s\"\n", search_results.cFileName);
316 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
317 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
318 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
319 search_results.dwFileAttributes);
321 ret = DeleteFileA( filename );
322 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
327 static void test__llseek( void )
329 INT i;
330 HFILE filehandle;
331 char buffer[1];
332 long bytes_read;
333 BOOL ret;
335 filehandle = _lcreat( filename, 0 );
336 if (filehandle == HFILE_ERROR)
338 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
339 return;
342 for (i = 0; i < 400; i++)
344 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
346 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
347 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
349 bytes_read = _hread( filehandle, buffer, 1);
350 ok( 1 == bytes_read, "file read size error\n" );
351 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
352 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
354 bytes_read = _hread( filehandle, buffer, 1);
355 ok( 1 == bytes_read, "file read size error\n" );
356 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
357 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
358 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
360 ret = DeleteFileA( filename );
361 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
365 static void test__llopen( void )
367 HFILE filehandle;
368 UINT bytes_read;
369 char buffer[10000];
370 BOOL ret;
372 filehandle = _lcreat( filename, 0 );
373 if (filehandle == HFILE_ERROR)
375 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
376 return;
379 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
380 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
382 filehandle = _lopen( filename, OF_READ );
383 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
384 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
385 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
386 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
388 filehandle = _lopen( filename, OF_READWRITE );
389 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
390 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
391 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
392 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
394 filehandle = _lopen( filename, OF_WRITE );
395 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
396 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
397 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
399 ret = DeleteFileA( filename );
400 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
401 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
405 static void test__lread( void )
407 HFILE filehandle;
408 char buffer[10000];
409 long bytes_read;
410 UINT bytes_wanted;
411 UINT i;
412 BOOL ret;
414 filehandle = _lcreat( filename, 0 );
415 if (filehandle == HFILE_ERROR)
417 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
418 return;
421 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
423 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
425 filehandle = _lopen( filename, OF_READ );
427 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
429 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
431 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
433 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
435 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
436 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
437 for (i = 0; i < bytes_wanted; i++)
439 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
443 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
445 ret = DeleteFileA( filename );
446 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
450 static void test__lwrite( void )
452 HFILE filehandle;
453 char buffer[10000];
454 long bytes_read;
455 long bytes_written;
456 long blocks;
457 long i;
458 char *contents;
459 HLOCAL memory_object;
460 char checksum[1];
461 BOOL ret;
463 filehandle = _lcreat( filename, 0 );
464 if (filehandle == HFILE_ERROR)
466 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
467 return;
470 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
472 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
474 filehandle = _lopen( filename, OF_READ );
476 bytes_read = _hread( filehandle, buffer, 1);
478 ok( 0 == bytes_read, "file read size error\n" );
480 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
482 filehandle = _lopen( filename, OF_READWRITE );
484 bytes_written = 0;
485 checksum[0] = '\0';
486 srand( (unsigned)time( NULL ) );
487 for (blocks = 0; blocks < 100; blocks++)
489 for (i = 0; i < (long)sizeof( buffer ); i++)
491 buffer[i] = rand( );
492 checksum[0] = checksum[0] + buffer[i];
494 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
495 bytes_written = bytes_written + sizeof( buffer );
498 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
499 bytes_written++;
501 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
503 memory_object = LocalAlloc( LPTR, bytes_written );
505 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
507 contents = LocalLock( memory_object );
509 filehandle = _lopen( filename, OF_READ );
511 contents = LocalLock( memory_object );
513 ok( NULL != contents, "LocalLock whines\n" );
515 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
517 checksum[0] = '\0';
518 i = 0;
521 checksum[0] += contents[i];
522 i++;
524 while (i < bytes_written - 1);
526 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
528 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
530 ret = DeleteFileA( filename );
531 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
534 static void test_CopyFileA(void)
536 char temp_path[MAX_PATH];
537 char source[MAX_PATH], dest[MAX_PATH];
538 static const char prefix[] = "pfx";
539 HANDLE hfile;
540 FILETIME ft1, ft2;
541 char buf[10];
542 DWORD ret;
543 BOOL retok;
545 ret = GetTempPathA(MAX_PATH, temp_path);
546 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
547 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
549 ret = GetTempFileNameA(temp_path, prefix, 0, source);
550 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
552 /* make the source have not zero size */
553 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
554 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
555 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
556 ok( retok && ret == sizeof(prefix),
557 "WriteFile error %d\n", GetLastError());
558 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
559 /* get the file time and change it to prove the difference */
560 ret = GetFileTime(hfile, NULL, NULL, &ft1);
561 ok( ret, "GetFileTime error %d\n", GetLastError());
562 ft1.dwLowDateTime -= 600000000; /* 60 second */
563 ret = SetFileTime(hfile, NULL, NULL, &ft1);
564 ok( ret, "SetFileTime error %d\n", GetLastError());
565 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
566 CloseHandle(hfile);
568 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
569 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
571 SetLastError(0xdeadbeef);
572 ret = CopyFileA(source, dest, TRUE);
573 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
574 "CopyFileA: unexpected error %d\n", GetLastError());
576 ret = CopyFileA(source, dest, FALSE);
577 ok(ret, "CopyFileA: error %d\n", GetLastError());
579 /* make sure that destination has correct size */
580 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
581 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
582 ret = GetFileSize(hfile, NULL);
583 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
585 /* make sure that destination has the same filetime */
586 ret = GetFileTime(hfile, NULL, NULL, &ft2);
587 ok( ret, "GetFileTime error %d\n", GetLastError());
588 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
590 SetLastError(0xdeadbeef);
591 ret = CopyFileA(source, dest, FALSE);
592 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
593 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
595 /* make sure that destination still has correct size */
596 ret = GetFileSize(hfile, NULL);
597 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
598 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
599 ok( retok && ret == sizeof(prefix),
600 "ReadFile: error %d\n", GetLastError());
601 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
602 CloseHandle(hfile);
604 ret = DeleteFileA(source);
605 ok(ret, "DeleteFileA: error %d\n", GetLastError());
606 ret = DeleteFileA(dest);
607 ok(ret, "DeleteFileA: error %d\n", GetLastError());
610 static void test_CopyFileW(void)
612 WCHAR temp_path[MAX_PATH];
613 WCHAR source[MAX_PATH], dest[MAX_PATH];
614 static const WCHAR prefix[] = {'p','f','x',0};
615 DWORD ret;
617 ret = GetTempPathW(MAX_PATH, temp_path);
618 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
619 return;
620 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
621 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
623 ret = GetTempFileNameW(temp_path, prefix, 0, source);
624 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
626 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
627 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
629 ret = CopyFileW(source, dest, TRUE);
630 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
631 "CopyFileW: unexpected error %d\n", GetLastError());
633 ret = CopyFileW(source, dest, FALSE);
634 ok(ret, "CopyFileW: error %d\n", GetLastError());
636 ret = DeleteFileW(source);
637 ok(ret, "DeleteFileW: error %d\n", GetLastError());
638 ret = DeleteFileW(dest);
639 ok(ret, "DeleteFileW: error %d\n", GetLastError());
642 static void test_CreateFileA(void)
644 HANDLE hFile;
645 char temp_path[MAX_PATH];
646 char filename[MAX_PATH];
647 static const char prefix[] = "pfx";
648 DWORD ret;
650 ret = GetTempPathA(MAX_PATH, temp_path);
651 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
652 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
654 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
655 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
657 SetLastError(0xdeadbeef);
658 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
659 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
660 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
661 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
663 SetLastError(0xdeadbeef);
664 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
665 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
666 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
667 "hFile %p, last error %u\n", hFile, GetLastError());
669 CloseHandle(hFile);
671 SetLastError(0xdeadbeef);
672 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
673 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
674 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
675 "hFile %p, last error %u\n", hFile, GetLastError());
677 CloseHandle(hFile);
679 ret = DeleteFileA(filename);
680 ok(ret, "DeleteFileA: error %d\n", GetLastError());
682 SetLastError(0xdeadbeef);
683 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
684 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
685 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
686 "hFile %p, last error %u\n", hFile, GetLastError());
688 CloseHandle(hFile);
690 ret = DeleteFileA(filename);
691 ok(ret, "DeleteFileA: error %d\n", GetLastError());
694 static void test_CreateFileW(void)
696 HANDLE hFile;
697 WCHAR temp_path[MAX_PATH];
698 WCHAR filename[MAX_PATH];
699 static const WCHAR emptyW[]={'\0'};
700 static const WCHAR prefix[] = {'p','f','x',0};
701 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
702 DWORD ret;
704 ret = GetTempPathW(MAX_PATH, temp_path);
705 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
706 return;
707 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
708 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
710 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
711 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
713 SetLastError(0xdeadbeef);
714 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
715 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
716 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
717 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
719 SetLastError(0xdeadbeef);
720 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
721 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
722 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
723 "hFile %p, last error %u\n", hFile, GetLastError());
725 CloseHandle(hFile);
727 SetLastError(0xdeadbeef);
728 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
729 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
730 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
731 "hFile %p, last error %u\n", hFile, GetLastError());
733 CloseHandle(hFile);
735 ret = DeleteFileW(filename);
736 ok(ret, "DeleteFileW: error %d\n", GetLastError());
738 SetLastError(0xdeadbeef);
739 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
740 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
741 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
742 "hFile %p, last error %u\n", hFile, GetLastError());
744 CloseHandle(hFile);
746 ret = DeleteFileW(filename);
747 ok(ret, "DeleteFileW: error %d\n", GetLastError());
749 if (0)
751 /* this crashes on NT4.0 */
752 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
753 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
754 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
755 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
758 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
759 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
760 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
761 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
763 /* test the result of opening a nonexistent driver name */
764 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
765 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
766 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
767 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
770 static void test_GetTempFileNameA(void)
772 UINT result;
773 char out[MAX_PATH];
774 char expected[MAX_PATH + 10];
775 char windowsdir[MAX_PATH + 10];
776 char windowsdrive[3];
778 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
779 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
780 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
782 /* If the Windows directory is the root directory, it ends in backslash, not else. */
783 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
785 strcat(windowsdir, "\\");
788 windowsdrive[0] = windowsdir[0];
789 windowsdrive[1] = windowsdir[1];
790 windowsdrive[2] = '\0';
792 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
793 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
794 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
795 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
796 windowsdrive[0], out);
798 result = GetTempFileNameA(windowsdir, "abc", 2, out);
799 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
800 expected[0] = '\0';
801 strcat(expected, windowsdir);
802 strcat(expected, "abc2.tmp");
803 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
804 out, expected);
807 static void test_DeleteFileA( void )
809 BOOL ret;
811 ret = DeleteFileA(NULL);
812 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
813 GetLastError() == ERROR_PATH_NOT_FOUND),
814 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
816 ret = DeleteFileA("");
817 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
818 GetLastError() == ERROR_BAD_PATHNAME),
819 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
821 ret = DeleteFileA("nul");
822 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
823 GetLastError() == ERROR_INVALID_PARAMETER ||
824 GetLastError() == ERROR_ACCESS_DENIED ||
825 GetLastError() == ERROR_INVALID_FUNCTION),
826 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
829 static void test_DeleteFileW( void )
831 BOOL ret;
832 static const WCHAR emptyW[]={'\0'};
834 ret = DeleteFileW(NULL);
835 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
836 return;
837 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
838 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
840 ret = DeleteFileW(emptyW);
841 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
842 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
845 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
847 static void test_MoveFileA(void)
849 char tempdir[MAX_PATH];
850 char source[MAX_PATH], dest[MAX_PATH];
851 static const char prefix[] = "pfx";
852 DWORD ret;
854 ret = GetTempPathA(MAX_PATH, tempdir);
855 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
856 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
858 ret = GetTempFileNameA(tempdir, prefix, 0, source);
859 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
861 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
862 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
864 ret = MoveFileA(source, dest);
865 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
866 "MoveFileA: unexpected error %d\n", GetLastError());
868 ret = DeleteFileA(dest);
869 ok(ret, "DeleteFileA: error %d\n", GetLastError());
871 ret = MoveFileA(source, dest);
872 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
874 lstrcatA(tempdir, "Remove Me");
875 ret = CreateDirectoryA(tempdir, NULL);
876 ok(ret == TRUE, "CreateDirectoryA failed\n");
878 lstrcpyA(source, dest);
879 lstrcpyA(dest, tempdir);
880 lstrcatA(dest, "\\wild?.*");
881 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
882 ret = MoveFileA(source, dest);
883 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
884 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
885 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
886 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
887 if (ret || (GetLastError() != ERROR_INVALID_NAME))
889 WIN32_FIND_DATAA fd;
890 char temppath[MAX_PATH];
891 HANDLE hFind;
893 lstrcpyA(temppath, tempdir);
894 lstrcatA(temppath, "\\*.*");
895 hFind = FindFirstFileA(temppath, &fd);
896 if (INVALID_HANDLE_VALUE != hFind)
898 LPSTR lpName;
901 lpName = fd.cAlternateFileName;
902 if (!lpName[0])
903 lpName = fd.cFileName;
904 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
906 while (FindNextFileA(hFind, &fd));
907 FindClose(hFind);
910 ret = DeleteFileA(source);
911 ok(ret, "DeleteFileA: error %d\n", GetLastError());
912 ret = DeleteFileA(dest);
913 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
914 ret = RemoveDirectoryA(tempdir);
915 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
918 static void test_MoveFileW(void)
920 WCHAR temp_path[MAX_PATH];
921 WCHAR source[MAX_PATH], dest[MAX_PATH];
922 static const WCHAR prefix[] = {'p','f','x',0};
923 DWORD ret;
925 ret = GetTempPathW(MAX_PATH, temp_path);
926 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
927 return;
928 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
929 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
931 ret = GetTempFileNameW(temp_path, prefix, 0, source);
932 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
934 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
935 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
937 ret = MoveFileW(source, dest);
938 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
939 "CopyFileW: unexpected error %d\n", GetLastError());
941 ret = DeleteFileW(source);
942 ok(ret, "DeleteFileW: error %d\n", GetLastError());
943 ret = DeleteFileW(dest);
944 ok(ret, "DeleteFileW: error %d\n", GetLastError());
947 #define PATTERN_OFFSET 0x10
949 static void test_offset_in_overlapped_structure(void)
951 HANDLE hFile;
952 OVERLAPPED ov;
953 DWORD done, offset;
954 BOOL rc;
955 BYTE buf[256], pattern[] = "TeSt";
956 UINT i;
957 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
958 BOOL ret;
960 ret =GetTempPathA(MAX_PATH, temp_path);
961 ok( ret, "GetTempPathA error %d\n", GetLastError());
962 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
963 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
965 /*** Write File *****************************************************/
967 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
968 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
970 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
971 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
972 ok( ret, "WriteFile error %d\n", GetLastError());
973 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
975 memset(&ov, 0, sizeof(ov));
976 S(U(ov)).Offset = PATTERN_OFFSET;
977 S(U(ov)).OffsetHigh = 0;
978 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
979 /* Win 9x does not support the overlapped I/O on files */
980 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
981 ok(rc, "WriteFile error %d\n", GetLastError());
982 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
983 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
984 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
986 S(U(ov)).Offset = sizeof(buf) * 2;
987 S(U(ov)).OffsetHigh = 0;
988 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
989 ok( ret, "WriteFile error %d\n", GetLastError());
990 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
991 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
992 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
995 CloseHandle(hFile);
997 /*** Read File *****************************************************/
999 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1000 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1002 memset(buf, 0, sizeof(buf));
1003 memset(&ov, 0, sizeof(ov));
1004 S(U(ov)).Offset = PATTERN_OFFSET;
1005 S(U(ov)).OffsetHigh = 0;
1006 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1007 /* Win 9x does not support the overlapped I/O on files */
1008 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1009 ok(rc, "ReadFile error %d\n", GetLastError());
1010 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1011 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1012 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1013 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1016 CloseHandle(hFile);
1018 ret = DeleteFileA(temp_fname);
1019 ok( ret, "DeleteFileA error %d\n", GetLastError());
1022 static void test_LockFile(void)
1024 HANDLE handle;
1025 DWORD written;
1026 OVERLAPPED overlapped;
1027 int limited_LockFile;
1028 int limited_UnLockFile;
1030 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1031 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1032 CREATE_ALWAYS, 0, 0 );
1033 if (handle == INVALID_HANDLE_VALUE)
1035 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1036 return;
1038 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1040 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1041 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1043 limited_UnLockFile = 0;
1044 if (UnlockFile( handle, 0, 0, 0, 0 ))
1046 limited_UnLockFile = 1;
1049 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1050 /* overlapping locks must fail */
1051 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1052 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1053 /* non-overlapping locks must succeed */
1054 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1056 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1057 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1058 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1059 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1061 S(U(overlapped)).Offset = 100;
1062 S(U(overlapped)).OffsetHigh = 0;
1063 overlapped.hEvent = 0;
1065 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1066 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1068 /* LockFileEx is probably OK, test it more. */
1069 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1070 "LockFileEx 100,100 failed\n" );
1073 /* overlapping shared locks are OK */
1074 S(U(overlapped)).Offset = 150;
1075 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1077 /* but exclusive is not */
1078 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1079 0, 50, 0, &overlapped ),
1080 "LockFileEx exclusive 150,50 succeeded\n" );
1081 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1082 { /* UnLockFile is capable. */
1083 S(U(overlapped)).Offset = 100;
1084 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1085 "UnlockFileEx 150,100 again succeeded\n" );
1088 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1089 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1090 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1091 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1093 /* wrap-around lock should not do anything */
1094 /* (but still succeeds on NT4 so we don't check result) */
1095 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1097 limited_LockFile = 0;
1098 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1100 limited_LockFile = 1;
1103 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1105 /* zero-byte lock */
1106 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1107 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1108 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1109 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1111 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1112 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1114 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1116 CloseHandle( handle );
1117 DeleteFileA( filename );
1120 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1122 if (!is_win9x)
1124 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1125 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1127 else
1129 access1 &= ~DELETE;
1130 if (!access1) access1 = GENERIC_READ;
1132 access2 &= ~DELETE;
1133 if (!access2) access2 = GENERIC_READ;
1136 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1137 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1138 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1139 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1140 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1141 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1142 return 1;
1145 static void test_file_sharing(void)
1147 static const DWORD access_modes[] =
1148 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1149 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1150 static const DWORD sharing_modes[] =
1151 { 0, FILE_SHARE_READ,
1152 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1153 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1154 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1155 int a1, s1, a2, s2;
1156 int ret;
1157 HANDLE h, h2;
1158 BOOL is_win9x = FALSE;
1160 /* make sure the file exists */
1161 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1162 if (h == INVALID_HANDLE_VALUE)
1164 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1165 return;
1167 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1168 CloseHandle( h );
1170 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1172 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1174 /* Win9x doesn't support FILE_SHARE_DELETE */
1175 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1176 continue;
1178 SetLastError(0xdeadbeef);
1179 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1180 NULL, OPEN_EXISTING, 0, 0 );
1181 if (h == INVALID_HANDLE_VALUE)
1183 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1184 return;
1186 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1188 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1190 /* Win9x doesn't support FILE_SHARE_DELETE */
1191 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1192 continue;
1194 SetLastError(0xdeadbeef);
1195 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1196 NULL, OPEN_EXISTING, 0, 0 );
1198 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1199 access_modes[a2], sharing_modes[s2], is_win9x ))
1201 ret = GetLastError();
1203 ok( h2 != INVALID_HANDLE_VALUE,
1204 "open failed for modes %x/%x/%x/%x\n",
1205 access_modes[a1], sharing_modes[s1],
1206 access_modes[a2], sharing_modes[s2] );
1207 ok( ret == 0xdeadbeef /* Win9x */ ||
1208 ret == 0, /* XP */
1209 "wrong error code %d\n", ret );
1211 CloseHandle( h2 );
1213 else
1215 ret = GetLastError();
1217 ok( h2 == INVALID_HANDLE_VALUE,
1218 "open succeeded for modes %x/%x/%x/%x\n",
1219 access_modes[a1], sharing_modes[s1],
1220 access_modes[a2], sharing_modes[s2] );
1221 ok( ret == ERROR_SHARING_VIOLATION,
1222 "wrong error code %d\n", ret );
1226 CloseHandle( h );
1230 SetLastError(0xdeadbeef);
1231 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1232 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1234 SetLastError(0xdeadbeef);
1235 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1236 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1237 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1239 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1240 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1242 CloseHandle(h);
1243 CloseHandle(h2);
1245 DeleteFileA( filename );
1248 static char get_windows_drive(void)
1250 char windowsdir[MAX_PATH];
1251 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1252 return windowsdir[0];
1255 static void test_FindFirstFileA(void)
1257 HANDLE handle;
1258 WIN32_FIND_DATAA data;
1259 int err;
1260 char buffer[5] = "C:\\";
1261 char buffer2[100];
1263 /* try FindFirstFileA on "C:\" */
1264 buffer[0] = get_windows_drive();
1266 SetLastError( 0xdeadbeaf );
1267 handle = FindFirstFileA(buffer, &data);
1268 err = GetLastError();
1269 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1270 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1272 /* try FindFirstFileA on "C:\*" */
1273 strcpy(buffer2, buffer);
1274 strcat(buffer2, "*");
1275 handle = FindFirstFileA(buffer2, &data);
1276 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1277 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1278 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1279 if (FindNextFileA( handle, &data ))
1280 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1281 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1282 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1284 /* try FindFirstFileA on windows dir */
1285 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1286 strcat(buffer2, "\\*");
1287 handle = FindFirstFileA(buffer2, &data);
1288 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1289 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1290 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1291 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1292 while (FindNextFileA( handle, &data ))
1293 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1294 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1295 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1297 /* try FindFirstFileA on "C:\foo\" */
1298 SetLastError( 0xdeadbeaf );
1299 strcpy(buffer2, buffer);
1300 strcat(buffer2, "foo\\");
1301 handle = FindFirstFileA(buffer2, &data);
1302 err = GetLastError();
1303 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1304 todo_wine {
1305 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1308 /* try FindFirstFileA on "C:\foo\bar.txt" */
1309 SetLastError( 0xdeadbeaf );
1310 strcpy(buffer2, buffer);
1311 strcat(buffer2, "foo\\bar.txt");
1312 handle = FindFirstFileA(buffer2, &data);
1313 err = GetLastError();
1314 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1315 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1317 /* try FindFirstFileA on "C:\foo\*.*" */
1318 SetLastError( 0xdeadbeaf );
1319 strcpy(buffer2, buffer);
1320 strcat(buffer2, "foo\\*.*");
1321 handle = FindFirstFileA(buffer2, &data);
1322 err = GetLastError();
1323 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1324 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1326 /* try FindFirstFileA on "foo\bar.txt" */
1327 SetLastError( 0xdeadbeaf );
1328 strcpy(buffer2, "foo\\bar.txt");
1329 handle = FindFirstFileA(buffer2, &data);
1330 err = GetLastError();
1331 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1332 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1334 /* try FindFirstFileA on "c:\nul" */
1335 SetLastError( 0xdeadbeaf );
1336 strcpy(buffer2, buffer);
1337 strcat(buffer2, "nul");
1338 handle = FindFirstFileA(buffer2, &data);
1339 err = GetLastError();
1340 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1341 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1342 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1343 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1344 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1345 SetLastError( 0xdeadbeaf );
1346 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1347 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1348 ok( FindClose( handle ), "failed to close handle\n" );
1350 /* try FindFirstFileA on "lpt1" */
1351 SetLastError( 0xdeadbeaf );
1352 strcpy(buffer2, "lpt1");
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, "lpt1"), "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 "c:\nul\*" */
1366 SetLastError( 0xdeadbeaf );
1367 strcpy(buffer2, buffer);
1368 strcat(buffer2, "nul\\*");
1369 handle = FindFirstFileA(buffer2, &data);
1370 err = GetLastError();
1371 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1372 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1374 /* try FindFirstFileA on "c:\nul*" */
1375 SetLastError( 0xdeadbeaf );
1376 strcpy(buffer2, buffer);
1377 strcat(buffer2, "nul*");
1378 handle = FindFirstFileA(buffer2, &data);
1379 err = GetLastError();
1380 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1381 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1383 /* try FindFirstFileA on "c:\foo\bar\nul" */
1384 SetLastError( 0xdeadbeaf );
1385 strcpy(buffer2, buffer);
1386 strcat(buffer2, "foo\\bar\\nul");
1387 handle = FindFirstFileA(buffer2, &data);
1388 err = GetLastError();
1389 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1390 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1392 /* try FindFirstFileA on "c:\foo\nul\bar" */
1393 SetLastError( 0xdeadbeaf );
1394 strcpy(buffer2, buffer);
1395 strcat(buffer2, "foo\\nul\\bar");
1396 handle = FindFirstFileA(buffer2, &data);
1397 err = GetLastError();
1398 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1399 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1402 static void test_FindNextFileA(void)
1404 HANDLE handle;
1405 WIN32_FIND_DATAA search_results;
1406 int err;
1407 char buffer[5] = "C:\\*";
1409 buffer[0] = get_windows_drive();
1410 handle = FindFirstFileA(buffer,&search_results);
1411 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1412 while (FindNextFile(handle, &search_results))
1414 /* get to the end of the files */
1416 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1417 err = GetLastError();
1418 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1421 static void test_FindFirstFileExA(void)
1423 WIN32_FIND_DATAA search_results;
1424 HANDLE handle;
1426 if (!pFindFirstFileExA)
1428 skip("FindFirstFileExA() is missing\n");
1429 return;
1432 CreateDirectoryA("test-dir", NULL);
1433 _lclose(_lcreat("test-dir\\file1", 0));
1434 _lclose(_lcreat("test-dir\\file2", 0));
1435 CreateDirectoryA("test-dir\\dir1", NULL);
1436 /* FindExLimitToDirectories is ignored */
1437 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, FindExSearchLimitToDirectories, NULL, 0);
1438 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1439 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1441 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1443 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1444 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1446 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1447 ok(CHECK_NAME(search_results.cFileName), "Invalid thrid entry - %s\n", search_results.cFileName);
1449 ok(FindNextFile(handle, &search_results), "Fetching fourth file failed\n");
1450 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1452 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1453 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1455 #undef CHECK_NAME
1457 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should failed\n");
1458 DeleteFileA("test-dir\\file1");
1459 DeleteFileA("test-dir\\file2");
1460 RemoveDirectoryA("test-dir\\dir1");
1461 RemoveDirectoryA("test-dir");
1464 static int test_Mapfile_createtemp(HANDLE *handle)
1466 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1467 DeleteFile(filename);
1468 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1469 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1470 if (*handle != INVALID_HANDLE_VALUE) {
1472 return 1;
1475 return 0;
1478 static void test_MapFile(void)
1480 HANDLE handle;
1481 HANDLE hmap;
1483 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1485 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1486 ok( hmap != NULL, "mapping should work, I named it!\n" );
1488 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1490 /* We have to close file before we try new stuff with mapping again.
1491 Else we would always succeed on XP or block descriptors on 95. */
1492 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1493 ok( hmap != NULL, "We should still be able to map!\n" );
1494 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1495 ok( CloseHandle( handle ), "can't close file handle\n");
1496 handle = NULL;
1498 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1500 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1501 ok( hmap == NULL, "mapped zero size file\n");
1502 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1504 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1505 ok( hmap == NULL, "mapping should fail\n");
1506 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1508 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1509 ok( hmap == NULL, "mapping should fail\n");
1510 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1512 /* On XP you can now map again, on Win 95 you cannot. */
1514 ok( CloseHandle( handle ), "can't close file handle\n");
1515 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1518 static void test_GetFileType(void)
1520 DWORD type;
1521 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1522 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1523 type = GetFileType(h);
1524 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1525 CloseHandle( h );
1526 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1527 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1528 type = GetFileType(h);
1529 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1530 CloseHandle( h );
1531 DeleteFileA( filename );
1534 static int completion_count;
1536 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1538 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1539 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1540 completion_count++;
1543 static void test_async_file_errors(void)
1545 char szFile[MAX_PATH];
1546 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1547 HANDLE hFile;
1548 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1549 OVERLAPPED ovl;
1550 S(U(ovl)).Offset = 0;
1551 S(U(ovl)).OffsetHigh = 0;
1552 ovl.hEvent = hSem;
1553 completion_count = 0;
1554 szFile[0] = '\0';
1555 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1556 strcat(szFile, "\\win.ini");
1557 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1558 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1559 while (TRUE)
1561 BOOL res;
1562 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1564 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1565 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1566 if (!res)
1567 break;
1568 S(U(ovl)).Offset += 4096;
1569 /* i/o completion routine only called if ReadFileEx returned success.
1570 * we only care about violations of this rule so undo what should have
1571 * been done */
1572 completion_count--;
1574 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1575 /*printf("Error = %ld\n", GetLastError());*/
1578 static void test_read_write(void)
1580 DWORD bytes, ret;
1581 HANDLE hFile;
1582 char temp_path[MAX_PATH];
1583 char filename[MAX_PATH];
1584 static const char prefix[] = "pfx";
1586 ret = GetTempPathA(MAX_PATH, temp_path);
1587 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1588 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1590 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1591 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1593 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1594 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1595 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1597 SetLastError(12345678);
1598 bytes = 12345678;
1599 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1600 ok(ret && GetLastError() == 12345678,
1601 "ret = %d, error %d\n", ret, GetLastError());
1602 ok(!bytes, "bytes = %d\n", bytes);
1604 SetLastError(12345678);
1605 bytes = 12345678;
1606 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1607 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1608 (ret && GetLastError() == 12345678), /* Win9x */
1609 "ret = %d, error %d\n", ret, GetLastError());
1610 ok(!bytes || /* Win2k */
1611 bytes == 10, /* Win9x */
1612 "bytes = %d\n", bytes);
1614 /* make sure the file contains data */
1615 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1616 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1618 SetLastError(12345678);
1619 bytes = 12345678;
1620 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1621 ok(ret && GetLastError() == 12345678,
1622 "ret = %d, error %d\n", ret, GetLastError());
1623 ok(!bytes, "bytes = %d\n", bytes);
1625 SetLastError(12345678);
1626 bytes = 12345678;
1627 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1628 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1629 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1630 "ret = %d, error %d\n", ret, GetLastError());
1631 ok(!bytes, "bytes = %d\n", bytes);
1633 ret = CloseHandle(hFile);
1634 ok( ret, "CloseHandle: error %d\n", GetLastError());
1635 ret = DeleteFileA(filename);
1636 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1639 static void test_OpenFile(void)
1641 HFILE hFile;
1642 OFSTRUCT ofs;
1643 BOOL ret;
1644 DWORD retval;
1646 static const char *file = "\\regsvr32.exe";
1647 static const char *foo = ".\\foo-bar-foo.baz";
1648 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1649 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1650 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1651 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1652 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1653 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1654 static const char *backslash = "\\";
1655 char buff[MAX_PATH];
1656 char buff_long[4*MAX_PATH];
1657 char filled_0xA5[OFS_MAXPATHNAME];
1658 UINT length;
1660 /* Check for existing file */
1661 length = GetSystemDirectoryA(buff, MAX_PATH);
1663 if (length + lstrlen(file) < MAX_PATH)
1665 lstrcatA(buff, file);
1666 memset(&ofs, 0xA5, sizeof(ofs));
1667 SetLastError(0xfaceabee);
1669 hFile = OpenFile(buff, &ofs, OF_EXIST);
1670 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1671 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1672 "GetLastError() returns %d\n", GetLastError() );
1673 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1674 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1675 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1676 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1677 ofs.szPathName, buff );
1680 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1681 length = GetCurrentDirectoryA(MAX_PATH, buff);
1683 /* Check for nonexistent file */
1684 if (length + lstrlenA(foo + 1) < MAX_PATH)
1686 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1687 memset(&ofs, 0xA5, sizeof(ofs));
1688 SetLastError(0xfaceabee);
1690 hFile = OpenFile(foo, &ofs, OF_EXIST);
1691 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1692 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1693 todo_wine
1694 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1695 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1696 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1697 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1698 ofs.szPathName, buff );
1701 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1702 length += lstrlenA(foo_too_long + 1);
1704 /* Check for nonexistent file with too long filename */
1705 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1707 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1708 memset(&ofs, 0xA5, sizeof(ofs));
1709 SetLastError(0xfaceabee);
1711 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1712 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1713 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1714 "GetLastError() returns %d\n", GetLastError() );
1715 todo_wine
1716 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1717 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1718 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1719 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1720 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1721 ofs.szPathName );
1724 length = GetCurrentDirectoryA(MAX_PATH, buff);
1725 length += lstrlenA(backslash);
1726 length += lstrlenA(filename);
1728 if (length >= MAX_PATH)
1730 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1731 return;
1733 lstrcatA(buff, backslash);
1734 lstrcatA(buff, filename);
1736 memset(&ofs, 0xA5, sizeof(ofs));
1737 SetLastError(0xfaceabee);
1738 /* Create an empty file */
1739 hFile = OpenFile(filename, &ofs, OF_CREATE);
1740 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1741 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1742 "GetLastError() returns %d\n", GetLastError() );
1743 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1744 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1745 ret = CloseHandle((HANDLE)hFile);
1746 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1747 retval = GetFileAttributesA(filename);
1748 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1750 memset(&ofs, 0xA5, sizeof(ofs));
1751 SetLastError(0xfaceabee);
1752 /* Check various opening options: */
1753 /* for reading only, */
1754 hFile = OpenFile(filename, &ofs, OF_READ);
1755 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1756 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1757 "GetLastError() returns %d\n", GetLastError() );
1758 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1759 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1760 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1761 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1762 ret = CloseHandle((HANDLE)hFile);
1763 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1765 memset(&ofs, 0xA5, sizeof(ofs));
1766 SetLastError(0xfaceabee);
1767 /* for writing only, */
1768 hFile = OpenFile(filename, &ofs, OF_WRITE);
1769 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1770 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1771 "GetLastError() returns %d\n", GetLastError() );
1772 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1773 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1774 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1775 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1776 ret = CloseHandle((HANDLE)hFile);
1777 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1779 memset(&ofs, 0xA5, sizeof(ofs));
1780 SetLastError(0xfaceabee);
1781 /* for reading and writing, */
1782 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1783 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1784 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1785 "GetLastError() returns %d\n", GetLastError() );
1786 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1787 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1788 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1789 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1790 ret = CloseHandle((HANDLE)hFile);
1791 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1793 memset(&ofs, 0xA5, sizeof(ofs));
1794 SetLastError(0xfaceabee);
1795 /* for checking file presence. */
1796 hFile = OpenFile(filename, &ofs, OF_EXIST);
1797 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1798 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1799 "GetLastError() returns %d\n", GetLastError() );
1800 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1801 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1802 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1803 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1805 memset(&ofs, 0xA5, sizeof(ofs));
1806 SetLastError(0xfaceabee);
1807 /* Delete the file and make sure it doesn't exist anymore */
1808 hFile = OpenFile(filename, &ofs, OF_DELETE);
1809 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1810 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1811 "GetLastError() returns %d\n", GetLastError() );
1812 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1813 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1814 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1815 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1817 retval = GetFileAttributesA(filename);
1818 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1821 static void test_overlapped(void)
1823 OVERLAPPED ov;
1824 DWORD r, result;
1826 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1828 memset( &ov, 0, sizeof ov );
1829 result = 1;
1830 r = GetOverlappedResult(0, &ov, &result, 0);
1831 ok( r == TRUE, "should return false\n");
1832 ok( result == 0, "wrong result %u\n", result );
1834 result = 0;
1835 ov.Internal = 0;
1836 ov.InternalHigh = 0xabcd;
1837 r = GetOverlappedResult(0, &ov, &result, 0);
1838 ok( r == TRUE, "should return false\n");
1839 ok( result == 0xabcd, "wrong result %u\n", result );
1841 SetLastError( 0xb00 );
1842 result = 0;
1843 ov.Internal = STATUS_INVALID_HANDLE;
1844 ov.InternalHigh = 0xabcd;
1845 r = GetOverlappedResult(0, &ov, &result, 0);
1846 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1847 ok( r == FALSE, "should return false\n");
1848 ok( result == 0xabcd, "wrong result %u\n", result );
1850 SetLastError( 0xb00 );
1851 result = 0;
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");
1857 ok( result == 0, "wrong result %u\n", result );
1859 SetLastError( 0xb00 );
1860 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
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");
1867 ResetEvent( ov.hEvent );
1869 SetLastError( 0xb00 );
1870 ov.Internal = STATUS_PENDING;
1871 ov.InternalHigh = 0;
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 r = CloseHandle( ov.hEvent );
1877 ok( r == TRUE, "close handle failed\n");
1880 static void test_RemoveDirectory(void)
1882 int rc;
1883 char directory[] = "removeme";
1885 rc = CreateDirectory(directory, NULL);
1886 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
1888 rc = SetCurrentDirectory(directory);
1889 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1891 rc = RemoveDirectory(".");
1892 todo_wine {
1893 ok( !rc, "RemoveDirectory unexpectedly worked\n" );
1896 rc = SetCurrentDirectory("..");
1897 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1899 rc = RemoveDirectory(directory);
1900 todo_wine {
1901 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1905 START_TEST(file)
1907 hkernel32 = GetModuleHandleA("kernel32.dll");
1908 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
1910 test__hread( );
1911 test__hwrite( );
1912 test__lclose( );
1913 test__lcreat( );
1914 test__llseek( );
1915 test__llopen( );
1916 test__lread( );
1917 test__lwrite( );
1918 test_GetTempFileNameA();
1919 test_CopyFileA();
1920 test_CopyFileW();
1921 test_CreateFileA();
1922 test_CreateFileW();
1923 test_DeleteFileA();
1924 test_DeleteFileW();
1925 test_MoveFileA();
1926 test_MoveFileW();
1927 test_FindFirstFileA();
1928 test_FindNextFileA();
1929 test_FindFirstFileExA();
1930 test_LockFile();
1931 test_file_sharing();
1932 test_offset_in_overlapped_structure();
1933 test_MapFile();
1934 test_GetFileType();
1935 test_async_file_errors();
1936 test_read_write();
1937 test_OpenFile();
1938 test_overlapped();
1939 test_RemoveDirectory();