Assorted spelling fixes.
[wine.git] / dlls / kernel / tests / file.c
blob1b63d91659afc4f5efc8f9313e2471efb081bd71
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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);
40 LPCSTR filename = "testfile.xxx";
41 LPCSTR sillytext =
42 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
54 static void test__hread( void )
56 HFILE filehandle;
57 char buffer[10000];
58 long bytes_read;
59 long bytes_wanted;
60 long i;
61 BOOL ret;
63 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
64 DeleteFileA( filename );
65 filehandle = _lcreat( filename, 0 );
66 if (filehandle == HFILE_ERROR)
68 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
69 return;
72 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
74 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
76 filehandle = _lopen( filename, OF_READ );
78 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError( ) );
80 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
82 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
84 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
86 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
87 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
88 for (i = 0; i < bytes_wanted; i++)
90 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
94 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
96 ret = DeleteFileA( filename );
97 ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
101 static void test__hwrite( void )
103 HFILE filehandle;
104 char buffer[10000];
105 long bytes_read;
106 long bytes_written;
107 long blocks;
108 long i;
109 char *contents;
110 HLOCAL memory_object;
111 char checksum[1];
112 BOOL ret;
114 filehandle = _lcreat( filename, 0 );
115 if (filehandle == HFILE_ERROR)
117 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
118 return;
121 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
123 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
125 filehandle = _lopen( filename, OF_READ );
127 bytes_read = _hread( filehandle, buffer, 1);
129 ok( 0 == bytes_read, "file read size error\n" );
131 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
133 filehandle = _lopen( filename, OF_READWRITE );
135 bytes_written = 0;
136 checksum[0] = '\0';
137 srand( (unsigned)time( NULL ) );
138 for (blocks = 0; blocks < 100; blocks++)
140 for (i = 0; i < (long)sizeof( buffer ); i++)
142 buffer[i] = rand( );
143 checksum[0] = checksum[0] + buffer[i];
145 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
146 bytes_written = bytes_written + sizeof( buffer );
149 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
150 bytes_written++;
152 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
154 memory_object = LocalAlloc( LPTR, bytes_written );
156 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
158 contents = LocalLock( memory_object );
160 filehandle = _lopen( filename, OF_READ );
162 contents = LocalLock( memory_object );
164 ok( NULL != contents, "LocalLock whines\n" );
166 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
168 checksum[0] = '\0';
169 i = 0;
172 checksum[0] = checksum[0] + contents[i];
173 i++;
175 while (i < bytes_written - 1);
177 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
179 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
181 ret = DeleteFileA( filename );
182 ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
186 static void test__lclose( void )
188 HFILE filehandle;
189 BOOL ret;
191 filehandle = _lcreat( filename, 0 );
192 if (filehandle == HFILE_ERROR)
194 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
195 return;
198 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
200 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
202 ret = DeleteFileA( filename );
203 ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
207 static void test__lcreat( void )
209 HFILE filehandle;
210 char buffer[10000];
211 WIN32_FIND_DATAA search_results;
212 char slashname[] = "testfi/";
213 int err;
214 HANDLE find;
215 BOOL ret;
217 filehandle = _lcreat( filename, 0 );
218 if (filehandle == HFILE_ERROR)
220 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
221 return;
224 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
226 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
228 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
230 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
232 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
234 ret = DeleteFileA(filename);
235 ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError());
237 filehandle = _lcreat( filename, 1 ); /* readonly */
238 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
240 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
242 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
244 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
246 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
248 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
250 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
252 filehandle = _lcreat( filename, 2 );
253 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
255 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
257 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
259 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
261 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
263 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
265 ret = DeleteFileA( filename );
266 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
268 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
269 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
271 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
273 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
275 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
277 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
279 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
281 ret = DeleteFileA( filename );
282 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
284 filehandle=_lcreat (slashname, 0); /* illegal name */
285 if (HFILE_ERROR==filehandle) {
286 err=GetLastError ();
287 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
288 "creating file \"%s\" failed with error %d\n", slashname, err);
289 } else { /* only NT succeeds */
290 _lclose(filehandle);
291 find=FindFirstFileA (slashname, &search_results);
292 if (INVALID_HANDLE_VALUE!=find)
294 ret = FindClose (find);
295 ok (0 != ret, "FindClose complains (%ld)\n", GetLastError ());
296 slashname[strlen(slashname)-1]=0;
297 ok (!strcmp (slashname, search_results.cFileName),
298 "found unexpected name \"%s\"\n", search_results.cFileName);
299 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
300 "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
301 search_results.dwFileAttributes);
303 ret = DeleteFileA( slashname );
304 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
307 filehandle=_lcreat (filename, 8); /* illegal attribute */
308 if (HFILE_ERROR==filehandle)
309 ok (0, "couldn't create volume label \"%s\"\n", filename);
310 else {
311 _lclose(filehandle);
312 find=FindFirstFileA (filename, &search_results);
313 if (INVALID_HANDLE_VALUE==find)
314 ok (0, "file \"%s\" not found\n", filename);
315 else {
316 ret = FindClose(find);
317 ok ( 0 != ret, "FindClose complains (%ld)\n", GetLastError ());
318 ok (!strcmp (filename, search_results.cFileName),
319 "found unexpected name \"%s\"\n", search_results.cFileName);
320 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
321 "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
322 search_results.dwFileAttributes);
324 ret = DeleteFileA( filename );
325 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
330 static void test__llseek( void )
332 INT i;
333 HFILE filehandle;
334 char buffer[1];
335 long bytes_read;
336 BOOL ret;
338 filehandle = _lcreat( filename, 0 );
339 if (filehandle == HFILE_ERROR)
341 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
342 return;
345 for (i = 0; i < 400; i++)
347 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
349 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
350 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
352 bytes_read = _hread( filehandle, buffer, 1);
353 ok( 1 == bytes_read, "file read size error\n" );
354 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
355 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
357 bytes_read = _hread( filehandle, buffer, 1);
358 ok( 1 == bytes_read, "file read size error\n" );
359 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
360 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
361 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
363 ret = DeleteFileA( filename );
364 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
368 static void test__llopen( void )
370 HFILE filehandle;
371 UINT bytes_read;
372 char buffer[10000];
373 BOOL ret;
375 filehandle = _lcreat( filename, 0 );
376 if (filehandle == HFILE_ERROR)
378 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
379 return;
382 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
383 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
385 filehandle = _lopen( filename, OF_READ );
386 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
387 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
388 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
389 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
391 filehandle = _lopen( filename, OF_READWRITE );
392 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
393 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
394 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
395 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
397 filehandle = _lopen( filename, OF_WRITE );
398 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
399 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
400 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
402 ret = DeleteFileA( filename );
403 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
404 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
408 static void test__lread( void )
410 HFILE filehandle;
411 char buffer[10000];
412 long bytes_read;
413 UINT bytes_wanted;
414 UINT i;
415 BOOL ret;
417 filehandle = _lcreat( filename, 0 );
418 if (filehandle == HFILE_ERROR)
420 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
421 return;
424 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
426 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
428 filehandle = _lopen( filename, OF_READ );
430 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError());
432 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
434 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
436 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
438 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
439 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
440 for (i = 0; i < bytes_wanted; i++)
442 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
446 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
448 ret = DeleteFileA( filename );
449 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
453 static void test__lwrite( void )
455 HFILE filehandle;
456 char buffer[10000];
457 long bytes_read;
458 long bytes_written;
459 long blocks;
460 long i;
461 char *contents;
462 HLOCAL memory_object;
463 char checksum[1];
464 BOOL ret;
466 filehandle = _lcreat( filename, 0 );
467 if (filehandle == HFILE_ERROR)
469 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
470 return;
473 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
475 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
477 filehandle = _lopen( filename, OF_READ );
479 bytes_read = _hread( filehandle, buffer, 1);
481 ok( 0 == bytes_read, "file read size error\n" );
483 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
485 filehandle = _lopen( filename, OF_READWRITE );
487 bytes_written = 0;
488 checksum[0] = '\0';
489 srand( (unsigned)time( NULL ) );
490 for (blocks = 0; blocks < 100; blocks++)
492 for (i = 0; i < (long)sizeof( buffer ); i++)
494 buffer[i] = rand( );
495 checksum[0] = checksum[0] + buffer[i];
497 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
498 bytes_written = bytes_written + sizeof( buffer );
501 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
502 bytes_written++;
504 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
506 memory_object = LocalAlloc( LPTR, bytes_written );
508 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
510 contents = LocalLock( memory_object );
512 filehandle = _lopen( filename, OF_READ );
514 contents = LocalLock( memory_object );
516 ok( NULL != contents, "LocalLock whines\n" );
518 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
520 checksum[0] = '\0';
521 i = 0;
524 checksum[0] += contents[i];
525 i++;
527 while (i < bytes_written - 1);
529 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
531 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
533 ret = DeleteFileA( filename );
534 ok( ret, "DeleteFile failed (%ld)\n", GetLastError( ) );
537 static void test_CopyFileA(void)
539 char temp_path[MAX_PATH];
540 char source[MAX_PATH], dest[MAX_PATH];
541 static const char prefix[] = "pfx";
542 HANDLE hfile;
543 char buf[10];
544 DWORD ret;
545 BOOL retok;
547 ret = GetTempPathA(MAX_PATH, temp_path);
548 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
549 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
551 ret = GetTempFileNameA(temp_path, prefix, 0, source);
552 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
554 /* make the source have not zero size */
555 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
556 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
557 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
558 ok( retok && ret == sizeof(prefix),
559 "WriteFile error %ld\n", GetLastError());
560 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
561 CloseHandle(hfile);
563 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
564 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
566 SetLastError(0xdeadbeef);
567 ret = CopyFileA(source, dest, TRUE);
568 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
569 "CopyFileA: unexpected error %ld\n", GetLastError());
571 ret = CopyFileA(source, dest, FALSE);
572 ok(ret, "CopyFileA: error %ld\n", GetLastError());
574 /* make sure that destination has correct size */
575 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
576 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
577 ret = GetFileSize(hfile, NULL);
578 ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
580 SetLastError(0xdeadbeef);
581 ret = CopyFileA(source, dest, FALSE);
582 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
583 "CopyFileA: ret = %ld, unexpected error %ld\n", ret, GetLastError());
585 /* make sure that destination still has correct size */
586 ret = GetFileSize(hfile, NULL);
587 ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
588 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
589 ok( retok && ret == sizeof(prefix),
590 "ReadFile: error %ld\n", GetLastError());
591 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
592 CloseHandle(hfile);
594 ret = DeleteFileA(source);
595 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
596 ret = DeleteFileA(dest);
597 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
600 static void test_CopyFileW(void)
602 WCHAR temp_path[MAX_PATH];
603 WCHAR source[MAX_PATH], dest[MAX_PATH];
604 static const WCHAR prefix[] = {'p','f','x',0};
605 DWORD ret;
607 ret = GetTempPathW(MAX_PATH, temp_path);
608 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
609 return;
610 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
611 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
613 ret = GetTempFileNameW(temp_path, prefix, 0, source);
614 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
616 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
617 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
619 ret = CopyFileW(source, dest, TRUE);
620 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
621 "CopyFileW: unexpected error %ld\n", GetLastError());
623 ret = CopyFileW(source, dest, FALSE);
624 ok(ret, "CopyFileW: error %ld\n", GetLastError());
626 ret = DeleteFileW(source);
627 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
628 ret = DeleteFileW(dest);
629 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
632 static void test_CreateFileA(void)
634 HANDLE hFile;
635 char temp_path[MAX_PATH];
636 char filename[MAX_PATH];
637 static const char prefix[] = "pfx";
638 DWORD ret;
640 ret = GetTempPathA(MAX_PATH, temp_path);
641 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
642 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
644 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
645 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
647 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
648 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
649 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
650 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
652 ret = DeleteFileA(filename);
653 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
656 static void test_CreateFileW(void)
658 HANDLE hFile;
659 WCHAR temp_path[MAX_PATH];
660 WCHAR filename[MAX_PATH];
661 static const WCHAR emptyW[]={'\0'};
662 static const WCHAR prefix[] = {'p','f','x',0};
663 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
664 DWORD ret;
666 ret = GetTempPathW(MAX_PATH, temp_path);
667 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
668 return;
669 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
670 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
672 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
673 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
675 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
676 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
677 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
678 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
680 ret = DeleteFileW(filename);
681 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
683 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
684 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
685 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
686 "CreateFileW(NULL) returned ret=%p error=%ld\n",hFile,GetLastError());
688 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
689 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
690 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
691 "CreateFileW(\"\") returned ret=%p error=%ld\n",hFile,GetLastError());
693 /* test the result of opening a nonexistent driver name */
694 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
695 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
696 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
697 "CreateFileW on invalid VxD name returned ret=%p error=%ld\n",hFile,GetLastError());
700 static void test_GetTempFileNameA()
702 UINT result;
703 char out[MAX_PATH];
704 char expected[MAX_PATH + 10];
705 char windowsdir[MAX_PATH + 10];
706 char windowsdrive[3];
708 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
709 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
710 ok(result != 0, "GetWindowsDirectory: error %ld\n", GetLastError());
712 /* If the Windows directory is the root directory, it ends in backslash, not else. */
713 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
715 strcat(windowsdir, "\\");
718 windowsdrive[0] = windowsdir[0];
719 windowsdrive[1] = windowsdir[1];
720 windowsdrive[2] = '\0';
722 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
723 ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
724 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
725 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
726 windowsdrive[0], out);
728 result = GetTempFileNameA(windowsdir, "abc", 2, out);
729 ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
730 expected[0] = '\0';
731 strcat(expected, windowsdir);
732 strcat(expected, "abc2.tmp");
733 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
734 out, expected);
737 static void test_DeleteFileA( void )
739 BOOL ret;
741 ret = DeleteFileA(NULL);
742 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
743 GetLastError() == ERROR_PATH_NOT_FOUND),
744 "DeleteFileA(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
746 ret = DeleteFileA("");
747 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
748 GetLastError() == ERROR_BAD_PATHNAME),
749 "DeleteFileA(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
751 ret = DeleteFileA("nul");
752 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
753 GetLastError() == ERROR_INVALID_PARAMETER ||
754 GetLastError() == ERROR_ACCESS_DENIED),
755 "DeleteFileA(\"nul\") returned ret=%d error=%ld\n",ret,GetLastError());
758 static void test_DeleteFileW( void )
760 BOOL ret;
761 static const WCHAR emptyW[]={'\0'};
763 ret = DeleteFileW(NULL);
764 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
765 return;
766 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
767 "DeleteFileW(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
769 ret = DeleteFileW(emptyW);
770 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
771 "DeleteFileW(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
774 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
776 static void test_MoveFileA(void)
778 char tempdir[MAX_PATH];
779 char source[MAX_PATH], dest[MAX_PATH];
780 static const char prefix[] = "pfx";
781 DWORD ret;
783 ret = GetTempPathA(MAX_PATH, tempdir);
784 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
785 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
787 ret = GetTempFileNameA(tempdir, prefix, 0, source);
788 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
790 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
791 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
793 ret = MoveFileA(source, dest);
794 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
795 "MoveFileA: unexpected error %ld\n", GetLastError());
797 ret = DeleteFileA(dest);
798 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
800 ret = MoveFileA(source, dest);
801 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
803 lstrcatA(tempdir, "Remove Me");
804 ret = CreateDirectoryA(tempdir, NULL);
805 ok(ret == TRUE, "CreateDirectoryA failed\n");
807 lstrcpyA(source, dest);
808 lstrcpyA(dest, tempdir);
809 lstrcatA(dest, "\\wild?.*");
810 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
811 ret = MoveFileA(source, dest);
812 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
813 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
814 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
815 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
816 if (ret || (GetLastError() != ERROR_INVALID_NAME))
818 WIN32_FIND_DATAA fd;
819 char temppath[MAX_PATH];
820 HANDLE hFind;
822 lstrcpyA(temppath, tempdir);
823 lstrcatA(temppath, "\\*.*");
824 hFind = FindFirstFileA(temppath, &fd);
825 if (INVALID_HANDLE_VALUE != hFind)
827 LPSTR lpName;
830 lpName = fd.cAlternateFileName;
831 if (!lpName[0])
832 lpName = fd.cFileName;
833 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
835 while (FindNextFileA(hFind, &fd));
836 FindClose(hFind);
839 ret = DeleteFileA(source);
840 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
841 ret = DeleteFileA(dest);
842 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
843 ret = RemoveDirectoryA(tempdir);
844 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
847 static void test_MoveFileW(void)
849 WCHAR temp_path[MAX_PATH];
850 WCHAR source[MAX_PATH], dest[MAX_PATH];
851 static const WCHAR prefix[] = {'p','f','x',0};
852 DWORD ret;
854 ret = GetTempPathW(MAX_PATH, temp_path);
855 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
856 return;
857 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
858 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
860 ret = GetTempFileNameW(temp_path, prefix, 0, source);
861 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
863 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
864 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
866 ret = MoveFileW(source, dest);
867 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
868 "CopyFileW: unexpected error %ld\n", GetLastError());
870 ret = DeleteFileW(source);
871 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
872 ret = DeleteFileW(dest);
873 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
876 #define PATTERN_OFFSET 0x10
878 static void test_offset_in_overlapped_structure(void)
880 HANDLE hFile;
881 OVERLAPPED ov;
882 DWORD done;
883 BOOL rc;
884 BYTE buf[256], pattern[] = "TeSt";
885 UINT i;
886 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
887 BOOL ret;
889 ret =GetTempPathA(MAX_PATH, temp_path);
890 ok( ret, "GetTempPathA error %ld\n", GetLastError());
891 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
892 ok( ret, "GetTempFileNameA error %ld\n", GetLastError());
894 /*** Write File *****************************************************/
896 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
897 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
899 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
900 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
901 ok( ret, "WriteFile error %ld\n", GetLastError());
902 ok(done == sizeof(buf), "expected number of bytes written %lu\n", done);
904 memset(&ov, 0, sizeof(ov));
905 ov.Offset = PATTERN_OFFSET;
906 ov.OffsetHigh = 0;
907 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
908 /* Win 9x does not support the overlapped I/O on files */
909 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
910 ok(rc, "WriteFile error %ld\n", GetLastError());
911 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
912 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
913 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
914 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
916 ov.Offset = sizeof(buf) * 2;
917 ov.OffsetHigh = 0;
918 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
919 ok( ret, "WriteFile error %ld\n", GetLastError());
920 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
921 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
922 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
923 "expected file offset %d\n", sizeof(buf) * 2 + sizeof(pattern));
926 CloseHandle(hFile);
928 /*** Read File *****************************************************/
930 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
931 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
933 memset(buf, 0, sizeof(buf));
934 memset(&ov, 0, sizeof(ov));
935 ov.Offset = PATTERN_OFFSET;
936 ov.OffsetHigh = 0;
937 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
938 /* Win 9x does not support the overlapped I/O on files */
939 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
940 ok(rc, "ReadFile error %ld\n", GetLastError());
941 ok(done == sizeof(pattern), "expected number of bytes read %lu\n", done);
942 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
943 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
944 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
945 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
948 CloseHandle(hFile);
950 ret = DeleteFileA(temp_fname);
951 ok( ret, "DeleteFileA error %ld\n", GetLastError());
954 static void test_LockFile(void)
956 HANDLE handle;
957 DWORD written;
958 OVERLAPPED overlapped;
959 int limited_LockFile;
960 int limited_UnLockFile;
961 int lockfileex_capable;
963 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
964 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
965 CREATE_ALWAYS, 0, 0 );
966 if (handle == INVALID_HANDLE_VALUE)
968 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
969 return;
971 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
973 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
974 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
976 limited_UnLockFile = 0;
977 if (UnlockFile( handle, 0, 0, 0, 0 ))
979 limited_UnLockFile = 1;
982 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
983 /* overlapping locks must fail */
984 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
985 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
986 /* non-overlapping locks must succeed */
987 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
989 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
990 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
991 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
992 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
994 overlapped.Offset = 100;
995 overlapped.OffsetHigh = 0;
996 overlapped.hEvent = 0;
998 lockfileex_capable = dll_capable("kernel32", "LockFileEx");
999 if (lockfileex_capable)
1001 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1002 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1004 /* LockFileEx is probably OK, test it more. */
1005 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1006 "LockFileEx 100,100 failed\n" );
1010 /* overlapping shared locks are OK */
1011 overlapped.Offset = 150;
1012 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1014 /* but exclusive is not */
1015 if (lockfileex_capable)
1017 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1018 0, 50, 0, &overlapped ),
1019 "LockFileEx exclusive 150,50 succeeded\n" );
1020 if (dll_capable("kernel32.dll", "UnlockFileEx"))
1022 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1023 { /* UnLockFile is capable. */
1024 overlapped.Offset = 100;
1025 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1026 "UnlockFileEx 150,100 again succeeded\n" );
1031 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1032 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1033 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1034 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1036 /* wrap-around lock should not do anything */
1037 /* (but still succeeds on NT4 so we don't check result) */
1038 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1040 limited_LockFile = 0;
1041 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1043 limited_LockFile = 1;
1046 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1048 /* zero-byte lock */
1049 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1050 limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1051 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1052 limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1054 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1055 !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1057 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1059 CloseHandle( handle );
1060 DeleteFileA( filename );
1063 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
1065 if (!access1 || !access2) return 1;
1066 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1067 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1068 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1069 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1070 return 1;
1073 static void test_file_sharing(void)
1075 static const DWORD access_modes[4] = { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE };
1076 static const DWORD sharing_modes[4] = { 0, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE };
1077 int a1, s1, a2, s2;
1078 int ret;
1080 /* make sure the file exists */
1081 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1082 CloseHandle( h );
1084 for (a1 = 0; a1 < 4; a1++)
1086 for (s1 = 0; s1 < 4; s1++)
1088 HANDLE h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1089 NULL, OPEN_EXISTING, 0, 0 );
1090 if (h == INVALID_HANDLE_VALUE)
1092 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
1093 return;
1095 for (a2 = 0; a2 < 4; a2++)
1097 for (s2 = 0; s2 < 4; s2++)
1099 HANDLE h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1100 NULL, OPEN_EXISTING, 0, 0 );
1101 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1102 access_modes[a2], sharing_modes[s2] ))
1104 ret = GetLastError();
1105 ok( ERROR_SHARING_VIOLATION == ret || 0 == ret,
1106 "Windows 95 sets GetLastError() = ERROR_SHARING_VIOLATION and\n"
1107 " Windows XP GetLastError() = 0, but now it is %d.\n"
1108 " indexes = %d, %d, %d, %d\n"
1109 " modes =\n %lx/%lx/%lx/%lx\n",
1110 ret,
1111 a1, s1, a2, s2,
1112 access_modes[a1], sharing_modes[s1],
1113 access_modes[a2], sharing_modes[s2]
1116 else
1118 ok( h2 == INVALID_HANDLE_VALUE,
1119 "open succeeded for modes %lx/%lx/%lx/%lx\n",
1120 access_modes[a1], sharing_modes[s1],
1121 access_modes[a2], sharing_modes[s2] );
1122 if (h2 == INVALID_HANDLE_VALUE)
1123 ok( GetLastError() == ERROR_SHARING_VIOLATION,
1124 "wrong error code %ld\n", GetLastError() );
1126 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1129 CloseHandle( h );
1132 DeleteFileA( filename );
1135 static char get_windows_drive()
1137 char windowsdir[MAX_PATH];
1138 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1139 return windowsdir[0];
1142 static void test_FindFirstFileA()
1144 HANDLE handle;
1145 WIN32_FIND_DATAA search_results;
1146 int err;
1147 char buffer[5] = "C:\\";
1149 /* try FindFirstFileA on "C:\" */
1150 buffer[0] = get_windows_drive();
1151 handle = FindFirstFileA(buffer,&search_results);
1152 err = GetLastError();
1153 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on root directory should Fail\n");
1154 if (handle == INVALID_HANDLE_VALUE)
1155 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
1157 /* try FindFirstFileA on "C:\*" */
1158 strcat(buffer, "*");
1159 handle = FindFirstFileA(buffer,&search_results);
1160 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer );
1161 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1164 static void test_FindNextFileA()
1166 HANDLE handle;
1167 WIN32_FIND_DATAA search_results;
1168 int err;
1169 char buffer[5] = "C:\\*";
1171 buffer[0] = get_windows_drive();
1172 handle = FindFirstFileA(buffer,&search_results);
1173 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1174 while (FindNextFile(handle, &search_results))
1176 /* get to the end of the files */
1178 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1179 err = GetLastError();
1180 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1183 static int test_Mapfile_createtemp(HANDLE *handle)
1185 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1186 DeleteFile(filename);
1187 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1188 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1189 if (*handle != INVALID_HANDLE_VALUE) {
1191 return 1;
1194 return 0;
1197 static void test_MapFile()
1199 HANDLE handle;
1200 HANDLE hmap;
1202 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1204 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1205 ok( hmap != NULL, "mapping should work, I named it!\n" );
1207 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1209 /* We have to close file before we try new stuff with mapping again.
1210 Else we would always succeed on XP or block descriptors on 95. */
1211 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1212 ok( hmap != NULL, "We should still be able to map!\n" );
1213 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1214 ok( CloseHandle( handle ), "can't close file handle\n");
1215 handle = NULL;
1217 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1219 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1220 ok( hmap == NULL, "mapped zero size file\n");
1221 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1223 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
1224 ok( hmap == NULL, "mapping should fail\n");
1225 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1227 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
1228 ok( hmap == NULL, "mapping should fail\n");
1229 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1231 /* On XP you can now map again, on Win 95 you can not. */
1233 ok( CloseHandle( handle ), "can't close file handle\n");
1234 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1237 static void test_GetFileType(void)
1239 DWORD type;
1240 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1241 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1242 type = GetFileType(h);
1243 ok( type == FILE_TYPE_DISK, "expected type disk got %ld\n", type );
1244 CloseHandle( h );
1245 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1246 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1247 type = GetFileType(h);
1248 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %ld\n", type );
1249 CloseHandle( h );
1250 DeleteFileA( filename );
1253 static int completion_count;
1255 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1257 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1258 ReleaseSemaphore(ovl->hEvent, 1, NULL);
1259 completion_count++;
1262 static void test_async_file_errors(void)
1264 char szFile[MAX_PATH];
1265 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1266 HANDLE hFile;
1267 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1268 OVERLAPPED ovl;
1269 ovl.Offset = 0;
1270 ovl.OffsetHigh = 0;
1271 ovl.hEvent = hSem;
1272 completion_count = 0;
1273 szFile[0] = '\0';
1274 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1275 strcat(szFile, "\\win.ini");
1276 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1277 ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1278 while (TRUE)
1280 BOOL res;
1281 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1283 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1284 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1285 if (!res)
1286 break;
1287 ovl.Offset += 4096;
1288 /* i/o completion routine only called if ReadFileEx returned success.
1289 * we only care about violations of this rule so undo what should have
1290 * been done */
1291 completion_count--;
1293 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1294 /*printf("Error = %ld\n", GetLastError());*/
1297 static void test_read_write(void)
1299 DWORD bytes, ret;
1300 HANDLE hFile;
1301 char temp_path[MAX_PATH];
1302 char filename[MAX_PATH];
1303 static const char prefix[] = "pfx";
1305 ret = GetTempPathA(MAX_PATH, temp_path);
1306 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
1307 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1309 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1310 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
1312 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1313 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1314 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %ld\n", GetLastError());
1316 SetLastError(12345678);
1317 bytes = 12345678;
1318 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1319 ok(ret && GetLastError() == 12345678,
1320 "ret = %ld, error %ld\n", ret, GetLastError());
1321 ok(!bytes, "bytes = %ld\n", bytes);
1323 SetLastError(12345678);
1324 bytes = 12345678;
1325 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1326 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1327 (ret && GetLastError() == 12345678), /* Win9x */
1328 "ret = %ld, error %ld\n", ret, GetLastError());
1329 ok(!bytes || /* Win2k */
1330 bytes == 10, /* Win9x */
1331 "bytes = %ld\n", bytes);
1333 SetLastError(12345678);
1334 bytes = 12345678;
1335 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1336 ok(ret && GetLastError() == 12345678,
1337 "ret = %ld, error %ld\n", ret, GetLastError());
1338 ok(!bytes, "bytes = %ld\n", bytes);
1340 SetLastError(12345678);
1341 bytes = 12345678;
1342 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1343 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1344 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1345 "ret = %ld, error %ld\n", ret, GetLastError());
1346 ok(!bytes, "bytes = %ld\n", bytes);
1348 ret = CloseHandle(hFile);
1349 ok( ret, "CloseHandle: error %ld\n", GetLastError());
1350 ret = DeleteFileA(filename);
1351 ok( ret, "DeleteFileA: error %ld\n", GetLastError());
1354 START_TEST(file)
1356 test__hread( );
1357 test__hwrite( );
1358 test__lclose( );
1359 test__lcreat( );
1360 test__llseek( );
1361 test__llopen( );
1362 test__lread( );
1363 test__lwrite( );
1364 test_GetTempFileNameA();
1365 test_CopyFileA();
1366 test_CopyFileW();
1367 test_CreateFileA();
1368 test_CreateFileW();
1369 test_DeleteFileA();
1370 test_DeleteFileW();
1371 test_MoveFileA();
1372 test_MoveFileW();
1373 test_FindFirstFileA();
1374 test_FindNextFileA();
1375 test_LockFile();
1376 test_file_sharing();
1377 test_offset_in_overlapped_structure();
1378 test_MapFile();
1379 test_GetFileType();
1380 test_async_file_errors();
1381 test_read_write();