2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002 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
25 #include "wine/test.h"
30 LPCSTR filename = "testfile.xxx";
32 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
33 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
34 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
35 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
36 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
37 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
38 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
39 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
40 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
41 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
44 static void test__hread( void )
52 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
53 DeleteFileA( filename );
54 filehandle = _lcreat( filename, 0 );
55 if (filehandle == HFILE_ERROR)
57 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
61 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
63 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
65 filehandle = _lopen( filename, OF_READ );
67 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError( ) );
69 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
71 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
73 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
75 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
76 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
77 for (i = 0; i < bytes_wanted; i++)
79 ok( buffer[i] == sillytext[i], "that's not what's written" );
83 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
85 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
89 static void test__hwrite( void )
101 filehandle = _lcreat( filename, 0 );
102 if (filehandle == HFILE_ERROR)
104 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
108 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
110 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
112 filehandle = _lopen( filename, OF_READ );
114 bytes_read = _hread( filehandle, buffer, 1);
116 ok( 0 == bytes_read, "file read size error" );
118 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
120 filehandle = _lopen( filename, OF_READWRITE );
124 srand( (unsigned)time( NULL ) );
125 for (blocks = 0; blocks < 100; blocks++)
127 for (i = 0; i < sizeof( buffer ); i++)
130 checksum[0] = checksum[0] + buffer[i];
132 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
133 bytes_written = bytes_written + sizeof( buffer );
136 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
139 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
141 memory_object = LocalAlloc( LPTR, bytes_written );
143 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
145 contents = LocalLock( memory_object );
147 filehandle = _lopen( filename, OF_READ );
149 contents = LocalLock( memory_object );
151 ok( NULL != contents, "LocalLock whines" );
153 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
159 checksum[0] = checksum[0] + contents[i];
162 while (i < bytes_written - 1);
164 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
166 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
168 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
172 static void test__lclose( void )
176 filehandle = _lcreat( filename, 0 );
177 if (filehandle == HFILE_ERROR)
179 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
183 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
185 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
187 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
191 static void test__lcreat( void )
195 WIN32_FIND_DATAA search_results;
196 char slashname[] = "testfi/";
199 filehandle = _lcreat( filename, 0 );
200 if (filehandle == HFILE_ERROR)
202 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
206 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
208 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
210 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
212 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
214 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
216 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
218 filehandle = _lcreat( filename, 1 ); /* readonly */
219 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
221 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
223 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
225 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
227 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
229 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
231 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
233 filehandle = _lcreat( filename, 2 );
234 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
236 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
238 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
240 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
242 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
244 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
246 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
248 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
249 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
251 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
253 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
255 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
257 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
259 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
261 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
263 filehandle=_lcreat (slashname, 0); /* illegal name */
264 if (HFILE_ERROR==filehandle)
265 ok (0, "couldn't create file \"%s\" (err=%ld)", slashname,
269 find=FindFirstFileA (slashname, &search_results);
270 if (INVALID_HANDLE_VALUE==find)
271 ok (0, "file \"%s\" not found", slashname);
273 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
274 slashname[strlen(slashname)-1]=0;
275 ok (!strcmp (slashname, search_results.cFileName),
276 "found unexpected name \"%s\"", search_results.cFileName);
277 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
278 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
279 search_results.dwFileAttributes);
281 ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
285 filehandle=_lcreat (filename, 8); /* illegal attribute */
286 if (HFILE_ERROR==filehandle)
287 ok (0, "couldn't create volume label \"%s\"", filename);
290 find=FindFirstFileA (filename, &search_results);
291 if (INVALID_HANDLE_VALUE==find)
292 ok (0, "file \"%s\" not found", filename);
294 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
295 ok (!strcmp (filename, search_results.cFileName),
296 "found unexpected name \"%s\"", search_results.cFileName);
297 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
298 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
299 search_results.dwFileAttributes);
301 ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
307 void test__llseek( void )
314 filehandle = _lcreat( filename, 0 );
315 if (filehandle == HFILE_ERROR)
317 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
321 for (i = 0; i < 400; i++)
323 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
325 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
326 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
328 bytes_read = _hread( filehandle, buffer, 1);
329 ok( 1 == bytes_read, "file read size error" );
330 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
331 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
333 bytes_read = _hread( filehandle, buffer, 1);
334 ok( 1 == bytes_read, "file read size error" );
335 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
336 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
337 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
339 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
343 static void test__llopen( void )
349 filehandle = _lcreat( filename, 0 );
350 if (filehandle == HFILE_ERROR)
352 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
356 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
357 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
359 filehandle = _lopen( filename, OF_READ );
360 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
361 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
362 ok( strlen( sillytext ) == bytes_read, "file read size error" );
363 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
365 filehandle = _lopen( filename, OF_READWRITE );
366 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
367 ok( strlen( sillytext ) == bytes_read, "file read size error" );
368 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
369 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
371 filehandle = _lopen( filename, OF_WRITE );
372 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
373 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
374 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
376 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
377 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
381 static void test__lread( void )
389 filehandle = _lcreat( filename, 0 );
390 if (filehandle == HFILE_ERROR)
392 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
396 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
398 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
400 filehandle = _lopen( filename, OF_READ );
402 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
404 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
406 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
408 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
410 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
411 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
412 for (i = 0; i < bytes_wanted; i++)
414 ok( buffer[i] == sillytext[i], "that's not what's written" );
418 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
420 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
424 static void test__lwrite( void )
433 HLOCAL memory_object;
436 filehandle = _lcreat( filename, 0 );
437 if (filehandle == HFILE_ERROR)
439 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
443 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
445 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
447 filehandle = _lopen( filename, OF_READ );
449 bytes_read = _hread( filehandle, buffer, 1);
451 ok( 0 == bytes_read, "file read size error" );
453 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
455 filehandle = _lopen( filename, OF_READWRITE );
459 srand( (unsigned)time( NULL ) );
460 for (blocks = 0; blocks < 100; blocks++)
462 for (i = 0; i < sizeof( buffer ); i++)
465 checksum[0] = checksum[0] + buffer[i];
467 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
468 bytes_written = bytes_written + sizeof( buffer );
471 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
474 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
476 memory_object = LocalAlloc( LPTR, bytes_written );
478 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
480 contents = LocalLock( memory_object );
482 filehandle = _lopen( filename, OF_READ );
484 contents = LocalLock( memory_object );
486 ok( NULL != contents, "LocalLock whines" );
488 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
494 checksum[0] += contents[i];
497 while (i < bytes_written - 1);
499 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
501 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
503 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
506 void test_CopyFileA(void)
508 char temp_path[MAX_PATH];
509 char source[MAX_PATH], dest[MAX_PATH];
510 static const char prefix[] = "pfx";
513 ret = GetTempPathA(MAX_PATH, temp_path);
514 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
515 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
517 ret = GetTempFileNameA(temp_path, prefix, 0, source);
518 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
520 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
521 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
523 ret = CopyFileA(source, dest, TRUE);
524 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
525 "CopyFileA: unexpected error %ld\n", GetLastError());
527 ret = CopyFileA(source, dest, FALSE);
528 ok(ret, "CopyFileA: error %ld\n", GetLastError());
530 ret = DeleteFileA(source);
531 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
532 ret = DeleteFileA(dest);
533 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
536 void test_CopyFileW(void)
538 WCHAR temp_path[MAX_PATH];
539 WCHAR source[MAX_PATH], dest[MAX_PATH];
540 static const WCHAR prefix[] = {'p','f','x',0};
543 ret = GetTempPathW(MAX_PATH, temp_path);
544 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
546 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
547 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
549 ret = GetTempFileNameW(temp_path, prefix, 0, source);
550 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
552 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
553 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
555 ret = CopyFileW(source, dest, TRUE);
556 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
557 "CopyFileW: unexpected error %ld\n", GetLastError());
559 ret = CopyFileW(source, dest, FALSE);
560 ok(ret, "CopyFileW: error %ld\n", GetLastError());
562 ret = DeleteFileW(source);
563 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
564 ret = DeleteFileW(dest);
565 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
568 void test_CreateFileA(void)
571 char temp_path[MAX_PATH];
572 char filename[MAX_PATH];
573 static const char prefix[] = "pfx";
576 ret = GetTempPathA(MAX_PATH, temp_path);
577 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
578 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
580 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
581 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
583 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
584 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
585 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
586 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
588 ret = DeleteFileA(filename);
589 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
592 void test_CreateFileW(void)
595 WCHAR temp_path[MAX_PATH];
596 WCHAR filename[MAX_PATH];
597 static const WCHAR prefix[] = {'p','f','x',0};
600 ret = GetTempPathW(MAX_PATH, temp_path);
601 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
603 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
604 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
606 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
607 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
609 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
610 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
611 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
612 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
614 ret = DeleteFileW(filename);
615 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
618 static void test_DeleteFileA( void )
622 ret = DeleteFileA(NULL);
623 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
624 GetLastError() == ERROR_PATH_NOT_FOUND),
625 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
627 ret = DeleteFileA("");
628 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
629 GetLastError() == ERROR_BAD_PATHNAME),
630 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
633 static void test_DeleteFileW( void )
636 WCHAR emptyW[]={'\0'};
638 ret = DeleteFileW(NULL);
639 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
641 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
642 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
644 ret = DeleteFileW(emptyW);
645 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
646 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
649 #define PATTERN_OFFSET 0x10
651 void test_offset_in_overlapped_structure(void)
657 BYTE buf[256], pattern[] = "TeSt";
659 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
661 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
662 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
664 /*** Write File *****************************************************/
666 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
667 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
669 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
670 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
671 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
673 memset(&ov, 0, sizeof(ov));
674 ov.Offset = PATTERN_OFFSET;
676 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
677 /* Win 9x does not support the overlapped I/O on files */
678 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
679 ok(rc, "WriteFile error %ld", GetLastError());
680 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
681 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
682 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
683 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
685 ov.Offset = sizeof(buf) * 2;
687 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
688 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
689 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
690 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
691 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
696 /*** Read File *****************************************************/
698 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
699 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
701 memset(buf, 0, sizeof(buf));
702 memset(&ov, 0, sizeof(ov));
703 ov.Offset = PATTERN_OFFSET;
705 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
706 /* Win 9x does not support the overlapped I/O on files */
707 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
708 ok(rc, "ReadFile error %ld", GetLastError());
709 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
710 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
711 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
712 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
713 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
718 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
721 static void test_LockFile(void)
725 OVERLAPPED overlapped;
727 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
728 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
729 CREATE_ALWAYS, 0, 0 );
730 if (handle == INVALID_HANDLE_VALUE)
732 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
735 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
737 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
738 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
739 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
741 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
742 /* overlapping locks must fail */
743 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
744 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
745 /* non-overlapping locks must succeed */
746 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
748 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
749 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
750 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
751 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
753 overlapped.Offset = 100;
754 overlapped.OffsetHigh = 0;
755 overlapped.hEvent = 0;
756 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
757 /* overlapping shared locks are OK */
758 overlapped.Offset = 150;
759 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
760 /* but exclusive is not */
761 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
762 "LockFileEx exclusive 150,50 succeeded" );
763 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
764 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
765 overlapped.Offset = 100;
766 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
767 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
769 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
770 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
771 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
772 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
774 /* wrap-around lock should not do anything */
775 /* (but still succeeds on NT4 so we don't check result) */
776 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
777 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
778 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
781 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
782 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
783 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
784 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
785 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
786 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
787 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
789 CloseHandle( handle );
790 DeleteFileA( filename );
793 void test_FindFirstFileA()
796 WIN32_FIND_DATAA search_results;
799 handle = FindFirstFileA("C:",&search_results);
800 err = GetLastError();
801 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
802 if (handle == INVALID_HANDLE_VALUE)
803 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
804 handle = FindFirstFileA("C:\\",&search_results);
805 err = GetLastError();
806 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
807 if (handle == INVALID_HANDLE_VALUE)
808 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
809 handle = FindFirstFileA("C:\\*",&search_results);
810 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
811 ok ( FindClose(handle) == TRUE, "Failed to close handle");
830 test_FindFirstFileA();
832 test_offset_in_overlapped_structure();