push caf8822204667872e3963bc36483c261826f5ad2
[wine/hacks.git] / dlls / kernel32 / tests / file.c
blob07869644baf4e2c792f5c03e1a0d9b8e61ef7a7b
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 int dll_capable(const char *dll, const char *function)
33 HMODULE module = GetModuleHandleA(dll);
34 if (!module) return 0;
36 return (GetProcAddress(module, function) != NULL);
39 /* keep filename and filenameW the same */
40 static const char filename[] = "testfile.xxx";
41 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
42 static const char sillytext[] =
43 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
55 static void test__hread( void )
57 HFILE filehandle;
58 char buffer[10000];
59 long bytes_read;
60 long bytes_wanted;
61 long i;
62 BOOL ret;
64 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
65 DeleteFileA( filename );
66 filehandle = _lcreat( filename, 0 );
67 if (filehandle == HFILE_ERROR)
69 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
70 return;
73 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
75 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
77 filehandle = _lopen( filename, OF_READ );
79 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
81 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
83 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
85 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
87 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
88 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
89 for (i = 0; i < bytes_wanted; i++)
91 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
95 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
97 ret = DeleteFileA( filename );
98 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
102 static void test__hwrite( void )
104 HFILE filehandle;
105 char buffer[10000];
106 long bytes_read;
107 long bytes_written;
108 long blocks;
109 long i;
110 char *contents;
111 HLOCAL memory_object;
112 char checksum[1];
113 BOOL ret;
115 filehandle = _lcreat( filename, 0 );
116 if (filehandle == HFILE_ERROR)
118 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
119 return;
122 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
124 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
126 filehandle = _lopen( filename, OF_READ );
128 bytes_read = _hread( filehandle, buffer, 1);
130 ok( 0 == bytes_read, "file read size error\n" );
132 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
134 filehandle = _lopen( filename, OF_READWRITE );
136 bytes_written = 0;
137 checksum[0] = '\0';
138 srand( (unsigned)time( NULL ) );
139 for (blocks = 0; blocks < 100; blocks++)
141 for (i = 0; i < (long)sizeof( buffer ); i++)
143 buffer[i] = rand( );
144 checksum[0] = checksum[0] + buffer[i];
146 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
147 bytes_written = bytes_written + sizeof( buffer );
150 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
151 bytes_written++;
153 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
155 memory_object = LocalAlloc( LPTR, bytes_written );
157 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
159 contents = LocalLock( memory_object );
161 filehandle = _lopen( filename, OF_READ );
163 contents = LocalLock( memory_object );
165 ok( NULL != contents, "LocalLock whines\n" );
167 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
169 checksum[0] = '\0';
170 i = 0;
173 checksum[0] = checksum[0] + contents[i];
174 i++;
176 while (i < bytes_written - 1);
178 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
180 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
182 ret = DeleteFileA( filename );
183 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
187 static void test__lclose( void )
189 HFILE filehandle;
190 BOOL ret;
192 filehandle = _lcreat( filename, 0 );
193 if (filehandle == HFILE_ERROR)
195 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
196 return;
199 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
201 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
203 ret = DeleteFileA( filename );
204 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
208 static void test__lcreat( void )
210 HFILE filehandle;
211 char buffer[10000];
212 WIN32_FIND_DATAA search_results;
213 char slashname[] = "testfi/";
214 int err;
215 HANDLE find;
216 BOOL ret;
218 filehandle = _lcreat( filename, 0 );
219 if (filehandle == HFILE_ERROR)
221 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
222 return;
225 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
227 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
229 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
231 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
233 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
235 ret = DeleteFileA(filename);
236 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
238 filehandle = _lcreat( filename, 1 ); /* readonly */
239 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
241 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
243 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
245 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
247 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
249 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
251 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
253 filehandle = _lcreat( filename, 2 );
254 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
256 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
258 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
260 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
262 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
264 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
266 ret = DeleteFileA( filename );
267 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
269 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
270 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
272 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
274 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
276 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
278 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
280 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
282 ret = DeleteFileA( filename );
283 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
285 filehandle=_lcreat (slashname, 0); /* illegal name */
286 if (HFILE_ERROR==filehandle) {
287 err=GetLastError ();
288 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
289 "creating file \"%s\" failed with error %d\n", slashname, err);
290 } else { /* only NT succeeds */
291 _lclose(filehandle);
292 find=FindFirstFileA (slashname, &search_results);
293 if (INVALID_HANDLE_VALUE!=find)
295 ret = FindClose (find);
296 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
297 slashname[strlen(slashname)-1]=0;
298 ok (!strcmp (slashname, search_results.cFileName),
299 "found unexpected name \"%s\"\n", search_results.cFileName);
300 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
302 search_results.dwFileAttributes);
304 ret = DeleteFileA( slashname );
305 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
308 filehandle=_lcreat (filename, 8); /* illegal attribute */
309 if (HFILE_ERROR==filehandle)
310 ok (0, "couldn't create volume label \"%s\"\n", filename);
311 else {
312 _lclose(filehandle);
313 find=FindFirstFileA (filename, &search_results);
314 if (INVALID_HANDLE_VALUE==find)
315 ok (0, "file \"%s\" not found\n", filename);
316 else {
317 ret = FindClose(find);
318 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
319 ok (!strcmp (filename, search_results.cFileName),
320 "found unexpected name \"%s\"\n", search_results.cFileName);
321 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
322 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
323 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
324 search_results.dwFileAttributes);
326 ret = DeleteFileA( filename );
327 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
332 static void test__llseek( void )
334 INT i;
335 HFILE filehandle;
336 char buffer[1];
337 long bytes_read;
338 BOOL ret;
340 filehandle = _lcreat( filename, 0 );
341 if (filehandle == HFILE_ERROR)
343 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
344 return;
347 for (i = 0; i < 400; i++)
349 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
351 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
352 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "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[27], "_llseek error, it got lost seeking\n" );
357 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
359 bytes_read = _hread( filehandle, buffer, 1);
360 ok( 1 == bytes_read, "file read size error\n" );
361 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
362 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
363 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
365 ret = DeleteFileA( filename );
366 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
370 static void test__llopen( void )
372 HFILE filehandle;
373 UINT bytes_read;
374 char buffer[10000];
375 BOOL ret;
377 filehandle = _lcreat( filename, 0 );
378 if (filehandle == HFILE_ERROR)
380 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
381 return;
384 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
385 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
387 filehandle = _lopen( filename, OF_READ );
388 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
389 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
390 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
391 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
393 filehandle = _lopen( filename, OF_READWRITE );
394 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
395 ok( strlen( sillytext ) == bytes_read, "file read size error\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 filehandle = _lopen( filename, OF_WRITE );
400 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
401 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
402 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
404 ret = DeleteFileA( filename );
405 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
406 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
410 static void test__lread( void )
412 HFILE filehandle;
413 char buffer[10000];
414 long bytes_read;
415 UINT bytes_wanted;
416 UINT i;
417 BOOL ret;
419 filehandle = _lcreat( filename, 0 );
420 if (filehandle == HFILE_ERROR)
422 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
423 return;
426 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
428 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
430 filehandle = _lopen( filename, OF_READ );
432 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
434 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
436 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
438 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
440 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
441 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
442 for (i = 0; i < bytes_wanted; i++)
444 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
448 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
450 ret = DeleteFileA( filename );
451 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
455 static void test__lwrite( void )
457 HFILE filehandle;
458 char buffer[10000];
459 long bytes_read;
460 long bytes_written;
461 long blocks;
462 long i;
463 char *contents;
464 HLOCAL memory_object;
465 char checksum[1];
466 BOOL ret;
468 filehandle = _lcreat( filename, 0 );
469 if (filehandle == HFILE_ERROR)
471 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
472 return;
475 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
477 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
479 filehandle = _lopen( filename, OF_READ );
481 bytes_read = _hread( filehandle, buffer, 1);
483 ok( 0 == bytes_read, "file read size error\n" );
485 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
487 filehandle = _lopen( filename, OF_READWRITE );
489 bytes_written = 0;
490 checksum[0] = '\0';
491 srand( (unsigned)time( NULL ) );
492 for (blocks = 0; blocks < 100; blocks++)
494 for (i = 0; i < (long)sizeof( buffer ); i++)
496 buffer[i] = rand( );
497 checksum[0] = checksum[0] + buffer[i];
499 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
500 bytes_written = bytes_written + sizeof( buffer );
503 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
504 bytes_written++;
506 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
508 memory_object = LocalAlloc( LPTR, bytes_written );
510 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
512 contents = LocalLock( memory_object );
514 filehandle = _lopen( filename, OF_READ );
516 contents = LocalLock( memory_object );
518 ok( NULL != contents, "LocalLock whines\n" );
520 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
522 checksum[0] = '\0';
523 i = 0;
526 checksum[0] += contents[i];
527 i++;
529 while (i < bytes_written - 1);
531 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
533 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
535 ret = DeleteFileA( filename );
536 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
539 static void test_CopyFileA(void)
541 char temp_path[MAX_PATH];
542 char source[MAX_PATH], dest[MAX_PATH];
543 static const char prefix[] = "pfx";
544 HANDLE hfile;
545 FILETIME ft1, ft2;
546 char buf[10];
547 DWORD ret;
548 BOOL retok;
550 ret = GetTempPathA(MAX_PATH, temp_path);
551 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
552 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
554 ret = GetTempFileNameA(temp_path, prefix, 0, source);
555 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
557 /* make the source have not zero size */
558 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
559 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
560 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
561 ok( retok && ret == sizeof(prefix),
562 "WriteFile error %d\n", GetLastError());
563 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
564 /* get the file time and change it to prove the difference */
565 ret = GetFileTime(hfile, NULL, NULL, &ft1);
566 ok( ret, "GetFileTime error %d\n", GetLastError());
567 ft1.dwLowDateTime -= 600000000; /* 60 second */
568 ret = SetFileTime(hfile, NULL, NULL, &ft1);
569 ok( ret, "SetFileTime error %d\n", GetLastError());
570 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
571 CloseHandle(hfile);
573 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
574 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
576 SetLastError(0xdeadbeef);
577 ret = CopyFileA(source, dest, TRUE);
578 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
579 "CopyFileA: unexpected error %d\n", GetLastError());
581 ret = CopyFileA(source, dest, FALSE);
582 ok(ret, "CopyFileA: error %d\n", GetLastError());
584 /* make sure that destination has correct size */
585 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
586 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
587 ret = GetFileSize(hfile, NULL);
588 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
590 /* make sure that destination has the same filetime */
591 ret = GetFileTime(hfile, NULL, NULL, &ft2);
592 ok( ret, "GetFileTime error %d\n", GetLastError());
593 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
595 SetLastError(0xdeadbeef);
596 ret = CopyFileA(source, dest, FALSE);
597 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
598 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
600 /* make sure that destination still has correct size */
601 ret = GetFileSize(hfile, NULL);
602 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
603 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
604 ok( retok && ret == sizeof(prefix),
605 "ReadFile: error %d\n", GetLastError());
606 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
607 CloseHandle(hfile);
609 ret = DeleteFileA(source);
610 ok(ret, "DeleteFileA: error %d\n", GetLastError());
611 ret = DeleteFileA(dest);
612 ok(ret, "DeleteFileA: error %d\n", GetLastError());
615 static void test_CopyFileW(void)
617 WCHAR temp_path[MAX_PATH];
618 WCHAR source[MAX_PATH], dest[MAX_PATH];
619 static const WCHAR prefix[] = {'p','f','x',0};
620 DWORD ret;
622 ret = GetTempPathW(MAX_PATH, temp_path);
623 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
624 return;
625 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
626 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
628 ret = GetTempFileNameW(temp_path, prefix, 0, source);
629 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
631 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
632 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
634 ret = CopyFileW(source, dest, TRUE);
635 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
636 "CopyFileW: unexpected error %d\n", GetLastError());
638 ret = CopyFileW(source, dest, FALSE);
639 ok(ret, "CopyFileW: error %d\n", GetLastError());
641 ret = DeleteFileW(source);
642 ok(ret, "DeleteFileW: error %d\n", GetLastError());
643 ret = DeleteFileW(dest);
644 ok(ret, "DeleteFileW: error %d\n", GetLastError());
647 static void test_CreateFileA(void)
649 HANDLE hFile;
650 char temp_path[MAX_PATH];
651 char filename[MAX_PATH];
652 static const char prefix[] = "pfx";
653 DWORD ret;
655 ret = GetTempPathA(MAX_PATH, temp_path);
656 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
657 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
659 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
660 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
662 SetLastError(0xdeadbeef);
663 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
664 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
665 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
666 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
668 SetLastError(0xdeadbeef);
669 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
670 CREATE_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());
674 CloseHandle(hFile);
676 SetLastError(0xdeadbeef);
677 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
678 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
679 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
680 "hFile %p, last error %u\n", hFile, GetLastError());
682 CloseHandle(hFile);
684 ret = DeleteFileA(filename);
685 ok(ret, "DeleteFileA: error %d\n", GetLastError());
687 SetLastError(0xdeadbeef);
688 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
689 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
690 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
691 "hFile %p, last error %u\n", hFile, GetLastError());
693 CloseHandle(hFile);
695 ret = DeleteFileA(filename);
696 ok(ret, "DeleteFileA: error %d\n", GetLastError());
699 static void test_CreateFileW(void)
701 HANDLE hFile;
702 WCHAR temp_path[MAX_PATH];
703 WCHAR filename[MAX_PATH];
704 static const WCHAR emptyW[]={'\0'};
705 static const WCHAR prefix[] = {'p','f','x',0};
706 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
707 DWORD ret;
709 ret = GetTempPathW(MAX_PATH, temp_path);
710 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
711 return;
712 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
713 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
715 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
716 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
718 SetLastError(0xdeadbeef);
719 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
720 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
721 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
722 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
724 SetLastError(0xdeadbeef);
725 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
726 CREATE_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());
730 CloseHandle(hFile);
732 SetLastError(0xdeadbeef);
733 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
734 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
735 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
736 "hFile %p, last error %u\n", hFile, GetLastError());
738 CloseHandle(hFile);
740 ret = DeleteFileW(filename);
741 ok(ret, "DeleteFileW: error %d\n", GetLastError());
743 SetLastError(0xdeadbeef);
744 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
745 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
746 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
747 "hFile %p, last error %u\n", hFile, GetLastError());
749 CloseHandle(hFile);
751 ret = DeleteFileW(filename);
752 ok(ret, "DeleteFileW: error %d\n", GetLastError());
754 if (0)
756 /* this crashes on NT4.0 */
757 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
758 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
759 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
760 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
763 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
764 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
765 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
766 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
768 /* test the result of opening a nonexistent driver name */
769 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
770 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
771 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
772 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
775 static void test_GetTempFileNameA(void)
777 UINT result;
778 char out[MAX_PATH];
779 char expected[MAX_PATH + 10];
780 char windowsdir[MAX_PATH + 10];
781 char windowsdrive[3];
783 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
784 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
785 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
787 /* If the Windows directory is the root directory, it ends in backslash, not else. */
788 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
790 strcat(windowsdir, "\\");
793 windowsdrive[0] = windowsdir[0];
794 windowsdrive[1] = windowsdir[1];
795 windowsdrive[2] = '\0';
797 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
798 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
799 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
800 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
801 windowsdrive[0], out);
803 result = GetTempFileNameA(windowsdir, "abc", 2, out);
804 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
805 expected[0] = '\0';
806 strcat(expected, windowsdir);
807 strcat(expected, "abc2.tmp");
808 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
809 out, expected);
812 static void test_DeleteFileA( void )
814 BOOL ret;
816 ret = DeleteFileA(NULL);
817 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
818 GetLastError() == ERROR_PATH_NOT_FOUND),
819 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
821 ret = DeleteFileA("");
822 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
823 GetLastError() == ERROR_BAD_PATHNAME),
824 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
826 ret = DeleteFileA("nul");
827 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
828 GetLastError() == ERROR_INVALID_PARAMETER ||
829 GetLastError() == ERROR_ACCESS_DENIED ||
830 GetLastError() == ERROR_INVALID_FUNCTION),
831 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
834 static void test_DeleteFileW( void )
836 BOOL ret;
837 static const WCHAR emptyW[]={'\0'};
839 ret = DeleteFileW(NULL);
840 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
841 return;
842 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
843 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
845 ret = DeleteFileW(emptyW);
846 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
847 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
850 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
852 static void test_MoveFileA(void)
854 char tempdir[MAX_PATH];
855 char source[MAX_PATH], dest[MAX_PATH];
856 static const char prefix[] = "pfx";
857 DWORD ret;
859 ret = GetTempPathA(MAX_PATH, tempdir);
860 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
861 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
863 ret = GetTempFileNameA(tempdir, prefix, 0, source);
864 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
866 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
867 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
869 ret = MoveFileA(source, dest);
870 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
871 "MoveFileA: unexpected error %d\n", GetLastError());
873 ret = DeleteFileA(dest);
874 ok(ret, "DeleteFileA: error %d\n", GetLastError());
876 ret = MoveFileA(source, dest);
877 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
879 lstrcatA(tempdir, "Remove Me");
880 ret = CreateDirectoryA(tempdir, NULL);
881 ok(ret == TRUE, "CreateDirectoryA failed\n");
883 lstrcpyA(source, dest);
884 lstrcpyA(dest, tempdir);
885 lstrcatA(dest, "\\wild?.*");
886 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
887 ret = MoveFileA(source, dest);
888 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
889 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
890 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
891 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
892 if (ret || (GetLastError() != ERROR_INVALID_NAME))
894 WIN32_FIND_DATAA fd;
895 char temppath[MAX_PATH];
896 HANDLE hFind;
898 lstrcpyA(temppath, tempdir);
899 lstrcatA(temppath, "\\*.*");
900 hFind = FindFirstFileA(temppath, &fd);
901 if (INVALID_HANDLE_VALUE != hFind)
903 LPSTR lpName;
906 lpName = fd.cAlternateFileName;
907 if (!lpName[0])
908 lpName = fd.cFileName;
909 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
911 while (FindNextFileA(hFind, &fd));
912 FindClose(hFind);
915 ret = DeleteFileA(source);
916 ok(ret, "DeleteFileA: error %d\n", GetLastError());
917 ret = DeleteFileA(dest);
918 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
919 ret = RemoveDirectoryA(tempdir);
920 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
923 static void test_MoveFileW(void)
925 WCHAR temp_path[MAX_PATH];
926 WCHAR source[MAX_PATH], dest[MAX_PATH];
927 static const WCHAR prefix[] = {'p','f','x',0};
928 DWORD ret;
930 ret = GetTempPathW(MAX_PATH, temp_path);
931 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
932 return;
933 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
934 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
936 ret = GetTempFileNameW(temp_path, prefix, 0, source);
937 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
939 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
940 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
942 ret = MoveFileW(source, dest);
943 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
944 "CopyFileW: unexpected error %d\n", GetLastError());
946 ret = DeleteFileW(source);
947 ok(ret, "DeleteFileW: error %d\n", GetLastError());
948 ret = DeleteFileW(dest);
949 ok(ret, "DeleteFileW: error %d\n", GetLastError());
952 #define PATTERN_OFFSET 0x10
954 static void test_offset_in_overlapped_structure(void)
956 HANDLE hFile;
957 OVERLAPPED ov;
958 DWORD done, offset;
959 BOOL rc;
960 BYTE buf[256], pattern[] = "TeSt";
961 UINT i;
962 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
963 BOOL ret;
965 ret =GetTempPathA(MAX_PATH, temp_path);
966 ok( ret, "GetTempPathA error %d\n", GetLastError());
967 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
968 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
970 /*** Write File *****************************************************/
972 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
973 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
975 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
976 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
977 ok( ret, "WriteFile error %d\n", GetLastError());
978 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
980 memset(&ov, 0, sizeof(ov));
981 S(U(ov)).Offset = PATTERN_OFFSET;
982 S(U(ov)).OffsetHigh = 0;
983 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
984 /* Win 9x does not support the overlapped I/O on files */
985 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
986 ok(rc, "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 == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
991 S(U(ov)).Offset = sizeof(buf) * 2;
992 S(U(ov)).OffsetHigh = 0;
993 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
994 ok( ret, "WriteFile error %d\n", GetLastError());
995 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
996 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
997 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1000 CloseHandle(hFile);
1002 /*** Read File *****************************************************/
1004 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1005 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1007 memset(buf, 0, sizeof(buf));
1008 memset(&ov, 0, sizeof(ov));
1009 S(U(ov)).Offset = PATTERN_OFFSET;
1010 S(U(ov)).OffsetHigh = 0;
1011 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1012 /* Win 9x does not support the overlapped I/O on files */
1013 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1014 ok(rc, "ReadFile error %d\n", GetLastError());
1015 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1016 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1017 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1018 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1021 CloseHandle(hFile);
1023 ret = DeleteFileA(temp_fname);
1024 ok( ret, "DeleteFileA error %d\n", GetLastError());
1027 static void test_LockFile(void)
1029 HANDLE handle;
1030 DWORD written;
1031 OVERLAPPED overlapped;
1032 int limited_LockFile;
1033 int limited_UnLockFile;
1034 int lockfileex_capable;
1036 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1037 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1038 CREATE_ALWAYS, 0, 0 );
1039 if (handle == INVALID_HANDLE_VALUE)
1041 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1042 return;
1044 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1046 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1047 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1049 limited_UnLockFile = 0;
1050 if (UnlockFile( handle, 0, 0, 0, 0 ))
1052 limited_UnLockFile = 1;
1055 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1056 /* overlapping locks must fail */
1057 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1058 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1059 /* non-overlapping locks must succeed */
1060 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1062 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1063 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1064 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1065 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1067 S(U(overlapped)).Offset = 100;
1068 S(U(overlapped)).OffsetHigh = 0;
1069 overlapped.hEvent = 0;
1071 lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1072 if (lockfileex_capable)
1074 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1075 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1077 /* LockFileEx is probably OK, test it more. */
1078 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1079 "LockFileEx 100,100 failed\n" );
1083 /* overlapping shared locks are OK */
1084 S(U(overlapped)).Offset = 150;
1085 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1087 /* but exclusive is not */
1088 if (lockfileex_capable)
1090 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1091 0, 50, 0, &overlapped ),
1092 "LockFileEx exclusive 150,50 succeeded\n" );
1093 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1095 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1096 { /* UnLockFile is capable. */
1097 S(U(overlapped)).Offset = 100;
1098 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1099 "UnlockFileEx 150,100 again succeeded\n" );
1104 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1105 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1106 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1107 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1109 /* wrap-around lock should not do anything */
1110 /* (but still succeeds on NT4 so we don't check result) */
1111 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1113 limited_LockFile = 0;
1114 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1116 limited_LockFile = 1;
1119 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1121 /* zero-byte lock */
1122 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1123 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1124 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1125 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1127 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1128 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1130 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1132 CloseHandle( handle );
1133 DeleteFileA( filename );
1136 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1138 if (!is_win9x)
1140 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1141 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1143 else
1145 access1 &= ~DELETE;
1146 if (!access1) access1 = GENERIC_READ;
1148 access2 &= ~DELETE;
1149 if (!access2) access2 = GENERIC_READ;
1152 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1153 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1154 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1155 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1156 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1157 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1158 return 1;
1161 static void test_file_sharing(void)
1163 static const DWORD access_modes[] =
1164 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1165 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1166 static const DWORD sharing_modes[] =
1167 { 0, FILE_SHARE_READ,
1168 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1169 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1170 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1171 int a1, s1, a2, s2;
1172 int ret;
1173 HANDLE h, h2;
1174 BOOL is_win9x = FALSE;
1176 /* make sure the file exists */
1177 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1178 if (h == INVALID_HANDLE_VALUE)
1180 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1181 return;
1183 is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1184 CloseHandle( h );
1186 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1188 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1190 /* Win9x doesn't support FILE_SHARE_DELETE */
1191 if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1192 continue;
1194 SetLastError(0xdeadbeef);
1195 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1196 NULL, OPEN_EXISTING, 0, 0 );
1197 if (h == INVALID_HANDLE_VALUE)
1199 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1200 return;
1202 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1204 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1206 /* Win9x doesn't support FILE_SHARE_DELETE */
1207 if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1208 continue;
1210 SetLastError(0xdeadbeef);
1211 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1212 NULL, OPEN_EXISTING, 0, 0 );
1214 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1215 access_modes[a2], sharing_modes[s2], is_win9x ))
1217 ret = GetLastError();
1219 ok( h2 != INVALID_HANDLE_VALUE,
1220 "open failed for modes %x/%x/%x/%x\n",
1221 access_modes[a1], sharing_modes[s1],
1222 access_modes[a2], sharing_modes[s2] );
1223 ok( ret == 0xdeadbeef /* Win9x */ ||
1224 ret == 0, /* XP */
1225 "wrong error code %d\n", ret );
1227 CloseHandle( h2 );
1229 else
1231 ret = GetLastError();
1233 ok( h2 == INVALID_HANDLE_VALUE,
1234 "open succeeded for modes %x/%x/%x/%x\n",
1235 access_modes[a1], sharing_modes[s1],
1236 access_modes[a2], sharing_modes[s2] );
1237 ok( ret == ERROR_SHARING_VIOLATION,
1238 "wrong error code %d\n", ret );
1242 CloseHandle( h );
1246 SetLastError(0xdeadbeef);
1247 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1248 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1250 SetLastError(0xdeadbeef);
1251 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1252 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1253 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1255 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1256 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1258 CloseHandle(h);
1259 CloseHandle(h2);
1261 DeleteFileA( filename );
1264 static char get_windows_drive(void)
1266 char windowsdir[MAX_PATH];
1267 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1268 return windowsdir[0];
1271 static void test_FindFirstFileA(void)
1273 HANDLE handle;
1274 WIN32_FIND_DATAA data;
1275 int err;
1276 char buffer[5] = "C:\\";
1277 char buffer2[100];
1279 /* try FindFirstFileA on "C:\" */
1280 buffer[0] = get_windows_drive();
1282 SetLastError( 0xdeadbeaf );
1283 handle = FindFirstFileA(buffer, &data);
1284 err = GetLastError();
1285 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1286 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1288 /* try FindFirstFileA on "C:\*" */
1289 strcpy(buffer2, buffer);
1290 strcat(buffer2, "*");
1291 handle = FindFirstFileA(buffer2, &data);
1292 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1293 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1294 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1295 if (FindNextFileA( handle, &data ))
1296 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1297 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1298 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1300 /* try FindFirstFileA on windows dir */
1301 GetWindowsDirectory( buffer2, sizeof(buffer2) );
1302 strcat(buffer2, "\\*");
1303 handle = FindFirstFileA(buffer2, &data);
1304 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1305 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1306 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1307 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1308 while (FindNextFileA( handle, &data ))
1309 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1310 "FindNextFile shouldn't return '%s'\n", data.cFileName );
1311 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1313 /* try FindFirstFileA on "C:\foo\" */
1314 SetLastError( 0xdeadbeaf );
1315 strcpy(buffer2, buffer);
1316 strcat(buffer2, "foo\\");
1317 handle = FindFirstFileA(buffer2, &data);
1318 err = GetLastError();
1319 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1320 todo_wine {
1321 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1324 /* try FindFirstFileA on "C:\foo\bar.txt" */
1325 SetLastError( 0xdeadbeaf );
1326 strcpy(buffer2, buffer);
1327 strcat(buffer2, "foo\\bar.txt");
1328 handle = FindFirstFileA(buffer2, &data);
1329 err = GetLastError();
1330 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1331 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1333 /* try FindFirstFileA on "C:\foo\*.*" */
1334 SetLastError( 0xdeadbeaf );
1335 strcpy(buffer2, buffer);
1336 strcat(buffer2, "foo\\*.*");
1337 handle = FindFirstFileA(buffer2, &data);
1338 err = GetLastError();
1339 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1340 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1342 /* try FindFirstFileA on "foo\bar.txt" */
1343 SetLastError( 0xdeadbeaf );
1344 strcpy(buffer2, "foo\\bar.txt");
1345 handle = FindFirstFileA(buffer2, &data);
1346 err = GetLastError();
1347 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1348 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1350 /* try FindFirstFileA on "c:\nul" */
1351 SetLastError( 0xdeadbeaf );
1352 strcpy(buffer2, buffer);
1353 strcat(buffer2, "nul");
1354 handle = FindFirstFileA(buffer2, &data);
1355 err = GetLastError();
1356 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1357 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1358 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1359 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1360 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1361 SetLastError( 0xdeadbeaf );
1362 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1363 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1364 ok( FindClose( handle ), "failed to close handle\n" );
1366 /* try FindFirstFileA on "lpt1" */
1367 SetLastError( 0xdeadbeaf );
1368 strcpy(buffer2, "lpt1");
1369 handle = FindFirstFileA(buffer2, &data);
1370 err = GetLastError();
1371 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1372 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1373 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1374 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1375 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1376 SetLastError( 0xdeadbeaf );
1377 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1378 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1379 ok( FindClose( handle ), "failed to close handle\n" );
1381 /* try FindFirstFileA on "c:\nul\*" */
1382 SetLastError( 0xdeadbeaf );
1383 strcpy(buffer2, buffer);
1384 strcat(buffer2, "nul\\*");
1385 handle = FindFirstFileA(buffer2, &data);
1386 err = GetLastError();
1387 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1388 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1390 /* try FindFirstFileA on "c:\nul*" */
1391 SetLastError( 0xdeadbeaf );
1392 strcpy(buffer2, buffer);
1393 strcat(buffer2, "nul*");
1394 handle = FindFirstFileA(buffer2, &data);
1395 err = GetLastError();
1396 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1397 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1399 /* try FindFirstFileA on "c:\foo\bar\nul" */
1400 SetLastError( 0xdeadbeaf );
1401 strcpy(buffer2, buffer);
1402 strcat(buffer2, "foo\\bar\\nul");
1403 handle = FindFirstFileA(buffer2, &data);
1404 err = GetLastError();
1405 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1406 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1408 /* try FindFirstFileA on "c:\foo\nul\bar" */
1409 SetLastError( 0xdeadbeaf );
1410 strcpy(buffer2, buffer);
1411 strcat(buffer2, "foo\\nul\\bar");
1412 handle = FindFirstFileA(buffer2, &data);
1413 err = GetLastError();
1414 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1415 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1418 static void test_FindNextFileA(void)
1420 HANDLE handle;
1421 WIN32_FIND_DATAA search_results;
1422 int err;
1423 char buffer[5] = "C:\\*";
1425 buffer[0] = get_windows_drive();
1426 handle = FindFirstFileA(buffer,&search_results);
1427 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1428 while (FindNextFile(handle, &search_results))
1430 /* get to the end of the files */
1432 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1433 err = GetLastError();
1434 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1437 static void test_FindFirstFileExA(void)
1439 WIN32_FIND_DATAA search_results;
1440 HANDLE handle;
1442 CreateDirectoryA("test-dir", NULL);
1443 _lclose(_lcreat("test-dir\\file1", 0));
1444 _lclose(_lcreat("test-dir\\file2", 0));
1445 CreateDirectoryA("test-dir\\dir1", NULL);
1446 /* FindExLimitToDirectories is ignored */
1447 handle = FindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, FindExSearchLimitToDirectories, NULL, 0);
1448 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1449 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1451 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1453 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1454 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1456 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1457 ok(CHECK_NAME(search_results.cFileName), "Invalid thrid entry - %s\n", search_results.cFileName);
1459 ok(FindNextFile(handle, &search_results), "Fetching fourth file failed\n");
1460 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1462 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1463 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1465 #undef CHECK_NAME
1467 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should failed\n");
1468 DeleteFileA("test-dir\\file1");
1469 DeleteFileA("test-dir\\file2");
1470 RemoveDirectoryA("test-dir\\dir1");
1471 RemoveDirectoryA("test-dir");
1474 static int test_Mapfile_createtemp(HANDLE *handle)
1476 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1477 DeleteFile(filename);
1478 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1479 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1480 if (*handle != INVALID_HANDLE_VALUE) {
1482 return 1;
1485 return 0;
1488 static void test_MapFile(void)
1490 HANDLE handle;
1491 HANDLE hmap;
1493 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1495 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1496 ok( hmap != NULL, "mapping should work, I named it!\n" );
1498 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1500 /* We have to close file before we try new stuff with mapping again.
1501 Else we would always succeed on XP or block descriptors on 95. */
1502 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1503 ok( hmap != NULL, "We should still be able to map!\n" );
1504 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1505 ok( CloseHandle( handle ), "can't close file handle\n");
1506 handle = NULL;
1508 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1510 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1511 ok( hmap == NULL, "mapped zero size file\n");
1512 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1514 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1515 ok( hmap == NULL, "mapping should fail\n");
1516 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1518 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1519 ok( hmap == NULL, "mapping should fail\n");
1520 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1522 /* On XP you can now map again, on Win 95 you cannot. */
1524 ok( CloseHandle( handle ), "can't close file handle\n");
1525 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1528 static void test_GetFileType(void)
1530 DWORD type;
1531 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1532 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1533 type = GetFileType(h);
1534 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1535 CloseHandle( h );
1536 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1537 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1538 type = GetFileType(h);
1539 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1540 CloseHandle( h );
1541 DeleteFileA( filename );
1544 static int completion_count;
1546 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1548 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1549 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1550 completion_count++;
1553 static void test_async_file_errors(void)
1555 char szFile[MAX_PATH];
1556 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1557 HANDLE hFile;
1558 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1559 OVERLAPPED ovl;
1560 S(U(ovl)).Offset = 0;
1561 S(U(ovl)).OffsetHigh = 0;
1562 ovl.hEvent = hSem;
1563 completion_count = 0;
1564 szFile[0] = '\0';
1565 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1566 strcat(szFile, "\\win.ini");
1567 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1568 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1569 while (TRUE)
1571 BOOL res;
1572 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1574 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1575 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1576 if (!res)
1577 break;
1578 S(U(ovl)).Offset += 4096;
1579 /* i/o completion routine only called if ReadFileEx returned success.
1580 * we only care about violations of this rule so undo what should have
1581 * been done */
1582 completion_count--;
1584 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1585 /*printf("Error = %ld\n", GetLastError());*/
1588 static void test_read_write(void)
1590 DWORD bytes, ret;
1591 HANDLE hFile;
1592 char temp_path[MAX_PATH];
1593 char filename[MAX_PATH];
1594 static const char prefix[] = "pfx";
1596 ret = GetTempPathA(MAX_PATH, temp_path);
1597 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1598 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1600 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1601 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1603 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1604 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1605 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1607 SetLastError(12345678);
1608 bytes = 12345678;
1609 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1610 ok(ret && GetLastError() == 12345678,
1611 "ret = %d, error %d\n", ret, GetLastError());
1612 ok(!bytes, "bytes = %d\n", bytes);
1614 SetLastError(12345678);
1615 bytes = 12345678;
1616 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1617 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1618 (ret && GetLastError() == 12345678), /* Win9x */
1619 "ret = %d, error %d\n", ret, GetLastError());
1620 ok(!bytes || /* Win2k */
1621 bytes == 10, /* Win9x */
1622 "bytes = %d\n", bytes);
1624 /* make sure the file contains data */
1625 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1626 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1628 SetLastError(12345678);
1629 bytes = 12345678;
1630 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1631 ok(ret && GetLastError() == 12345678,
1632 "ret = %d, error %d\n", ret, GetLastError());
1633 ok(!bytes, "bytes = %d\n", bytes);
1635 SetLastError(12345678);
1636 bytes = 12345678;
1637 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1638 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1639 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1640 "ret = %d, error %d\n", ret, GetLastError());
1641 ok(!bytes, "bytes = %d\n", bytes);
1643 ret = CloseHandle(hFile);
1644 ok( ret, "CloseHandle: error %d\n", GetLastError());
1645 ret = DeleteFileA(filename);
1646 ok( ret, "DeleteFileA: error %d\n", GetLastError());
1649 static void test_OpenFile(void)
1651 HFILE hFile;
1652 OFSTRUCT ofs;
1653 BOOL ret;
1654 DWORD retval;
1656 static const char *file = "\\regsvr32.exe";
1657 static const char *foo = ".\\foo-bar-foo.baz";
1658 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1659 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1660 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1661 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1662 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1663 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1664 static const char *backslash = "\\";
1665 char buff[MAX_PATH];
1666 char buff_long[4*MAX_PATH];
1667 char filled_0xA5[OFS_MAXPATHNAME];
1668 UINT length;
1670 /* Check for existing file */
1671 length = GetSystemDirectoryA(buff, MAX_PATH);
1673 if (length + lstrlen(file) < MAX_PATH)
1675 lstrcatA(buff, file);
1676 memset(&ofs, 0xA5, sizeof(ofs));
1677 SetLastError(0xfaceabee);
1679 hFile = OpenFile(buff, &ofs, OF_EXIST);
1680 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1681 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1682 "GetLastError() returns %d\n", GetLastError() );
1683 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1684 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1685 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1686 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1687 ofs.szPathName, buff );
1690 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1691 length = GetCurrentDirectoryA(MAX_PATH, buff);
1693 /* Check for nonexistent file */
1694 if (length + lstrlenA(foo + 1) < MAX_PATH)
1696 lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1697 memset(&ofs, 0xA5, sizeof(ofs));
1698 SetLastError(0xfaceabee);
1700 hFile = OpenFile(foo, &ofs, OF_EXIST);
1701 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1702 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1703 todo_wine
1704 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1705 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1706 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1707 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1708 ofs.szPathName, buff );
1711 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1712 length += lstrlenA(foo_too_long + 1);
1714 /* Check for nonexistent file with too long filename */
1715 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
1717 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1718 memset(&ofs, 0xA5, sizeof(ofs));
1719 SetLastError(0xfaceabee);
1721 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1722 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1723 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
1724 "GetLastError() returns %d\n", GetLastError() );
1725 todo_wine
1726 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1727 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1728 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1729 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1730 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
1731 ofs.szPathName );
1734 length = GetCurrentDirectoryA(MAX_PATH, buff);
1735 length += lstrlenA(backslash);
1736 length += lstrlenA(filename);
1738 if (length >= MAX_PATH)
1740 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
1741 return;
1743 lstrcatA(buff, backslash);
1744 lstrcatA(buff, filename);
1746 memset(&ofs, 0xA5, sizeof(ofs));
1747 SetLastError(0xfaceabee);
1748 /* Create an empty file */
1749 hFile = OpenFile(filename, &ofs, OF_CREATE);
1750 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1751 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1752 "GetLastError() returns %d\n", GetLastError() );
1753 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1754 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1755 ret = CloseHandle((HANDLE)hFile);
1756 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1757 retval = GetFileAttributesA(filename);
1758 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1760 memset(&ofs, 0xA5, sizeof(ofs));
1761 SetLastError(0xfaceabee);
1762 /* Check various opening options: */
1763 /* for reading only, */
1764 hFile = OpenFile(filename, &ofs, OF_READ);
1765 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1766 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1767 "GetLastError() returns %d\n", GetLastError() );
1768 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1769 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1770 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1771 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1772 ret = CloseHandle((HANDLE)hFile);
1773 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1775 memset(&ofs, 0xA5, sizeof(ofs));
1776 SetLastError(0xfaceabee);
1777 /* for writing only, */
1778 hFile = OpenFile(filename, &ofs, OF_WRITE);
1779 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1780 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1781 "GetLastError() returns %d\n", GetLastError() );
1782 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1783 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1784 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1785 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1786 ret = CloseHandle((HANDLE)hFile);
1787 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1789 memset(&ofs, 0xA5, sizeof(ofs));
1790 SetLastError(0xfaceabee);
1791 /* for reading and writing, */
1792 hFile = OpenFile(filename, &ofs, OF_READWRITE);
1793 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1794 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1795 "GetLastError() returns %d\n", GetLastError() );
1796 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1797 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1798 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1799 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1800 ret = CloseHandle((HANDLE)hFile);
1801 ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1803 memset(&ofs, 0xA5, sizeof(ofs));
1804 SetLastError(0xfaceabee);
1805 /* for checking file presence. */
1806 hFile = OpenFile(filename, &ofs, OF_EXIST);
1807 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1808 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1809 "GetLastError() returns %d\n", GetLastError() );
1810 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1811 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1812 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1813 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1815 memset(&ofs, 0xA5, sizeof(ofs));
1816 SetLastError(0xfaceabee);
1817 /* Delete the file and make sure it doesn't exist anymore */
1818 hFile = OpenFile(filename, &ofs, OF_DELETE);
1819 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1820 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
1821 "GetLastError() returns %d\n", GetLastError() );
1822 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1823 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1824 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1825 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1827 retval = GetFileAttributesA(filename);
1828 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1831 static void test_overlapped(void)
1833 OVERLAPPED ov;
1834 DWORD r, result;
1836 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1838 memset( &ov, 0, sizeof ov );
1839 result = 1;
1840 r = GetOverlappedResult(0, &ov, &result, 0);
1841 ok( r == TRUE, "should return false\n");
1842 ok( result == 0, "wrong result %u\n", result );
1844 result = 0;
1845 ov.Internal = 0;
1846 ov.InternalHigh = 0xabcd;
1847 r = GetOverlappedResult(0, &ov, &result, 0);
1848 ok( r == TRUE, "should return false\n");
1849 ok( result == 0xabcd, "wrong result %u\n", result );
1851 SetLastError( 0xb00 );
1852 result = 0;
1853 ov.Internal = STATUS_INVALID_HANDLE;
1854 ov.InternalHigh = 0xabcd;
1855 r = GetOverlappedResult(0, &ov, &result, 0);
1856 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1857 ok( r == FALSE, "should return false\n");
1858 ok( result == 0xabcd, "wrong result %u\n", result );
1860 SetLastError( 0xb00 );
1861 result = 0;
1862 ov.Internal = STATUS_PENDING;
1863 ov.InternalHigh = 0xabcd;
1864 r = GetOverlappedResult(0, &ov, &result, 0);
1865 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1866 ok( r == FALSE, "should return false\n");
1867 ok( result == 0, "wrong result %u\n", result );
1869 SetLastError( 0xb00 );
1870 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1871 ov.Internal = STATUS_PENDING;
1872 ov.InternalHigh = 0xabcd;
1873 r = GetOverlappedResult(0, &ov, &result, 0);
1874 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1875 ok( r == FALSE, "should return false\n");
1877 ResetEvent( ov.hEvent );
1879 SetLastError( 0xb00 );
1880 ov.Internal = STATUS_PENDING;
1881 ov.InternalHigh = 0;
1882 r = GetOverlappedResult(0, &ov, &result, 0);
1883 ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1884 ok( r == FALSE, "should return false\n");
1886 r = CloseHandle( ov.hEvent );
1887 ok( r == TRUE, "close handle failed\n");
1890 static void test_RemoveDirectory(void)
1892 int rc;
1893 char directory[] = "removeme";
1895 rc = CreateDirectory(directory, NULL);
1896 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
1898 rc = SetCurrentDirectory(directory);
1899 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1901 rc = RemoveDirectory(".");
1902 todo_wine {
1903 ok( !rc, "RemoveDirectory unexpectedly worked\n" );
1906 rc = SetCurrentDirectory("..");
1907 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
1909 rc = RemoveDirectory(directory);
1910 todo_wine {
1911 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
1915 START_TEST(file)
1917 test__hread( );
1918 test__hwrite( );
1919 test__lclose( );
1920 test__lcreat( );
1921 test__llseek( );
1922 test__llopen( );
1923 test__lread( );
1924 test__lwrite( );
1925 test_GetTempFileNameA();
1926 test_CopyFileA();
1927 test_CopyFileW();
1928 test_CreateFileA();
1929 test_CreateFileW();
1930 test_DeleteFileA();
1931 test_DeleteFileW();
1932 test_MoveFileA();
1933 test_MoveFileW();
1934 test_FindFirstFileA();
1935 test_FindNextFileA();
1936 test_FindFirstFileExA();
1937 test_LockFile();
1938 test_file_sharing();
1939 test_offset_in_overlapped_structure();
1940 test_MapFile();
1941 test_GetFileType();
1942 test_async_file_errors();
1943 test_read_write();
1944 test_OpenFile();
1945 test_overlapped();
1946 test_RemoveDirectory();