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
26 #include "wine/test.h"
31 LPCSTR filename = "testfile.xxx";
33 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
42 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
45 static void test__hread( void )
53 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
54 DeleteFileA( filename );
55 filehandle = _lcreat( filename, 0 );
56 if (filehandle == HFILE_ERROR)
58 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
62 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
64 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
66 filehandle = _lopen( filename, OF_READ );
68 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError( ) );
70 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
72 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
74 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
76 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
77 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
78 for (i = 0; i < bytes_wanted; i++)
80 ok( buffer[i] == sillytext[i], "that's not what's written" );
84 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
86 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
90 static void test__hwrite( void )
102 filehandle = _lcreat( filename, 0 );
103 if (filehandle == HFILE_ERROR)
105 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
109 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
111 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
113 filehandle = _lopen( filename, OF_READ );
115 bytes_read = _hread( filehandle, buffer, 1);
117 ok( 0 == bytes_read, "file read size error" );
119 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
121 filehandle = _lopen( filename, OF_READWRITE );
125 srand( (unsigned)time( NULL ) );
126 for (blocks = 0; blocks < 100; blocks++)
128 for (i = 0; i < (long)sizeof( buffer ); i++)
131 checksum[0] = checksum[0] + buffer[i];
133 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
134 bytes_written = bytes_written + sizeof( buffer );
137 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
140 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
142 memory_object = LocalAlloc( LPTR, bytes_written );
144 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
146 contents = LocalLock( memory_object );
148 filehandle = _lopen( filename, OF_READ );
150 contents = LocalLock( memory_object );
152 ok( NULL != contents, "LocalLock whines" );
154 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
160 checksum[0] = checksum[0] + contents[i];
163 while (i < bytes_written - 1);
165 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
167 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
169 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
173 static void test__lclose( void )
177 filehandle = _lcreat( filename, 0 );
178 if (filehandle == HFILE_ERROR)
180 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
184 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
186 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
188 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
192 static void test__lcreat( void )
196 WIN32_FIND_DATAA search_results;
197 char slashname[] = "testfi/";
201 filehandle = _lcreat( filename, 0 );
202 if (filehandle == HFILE_ERROR)
204 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
208 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
210 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
212 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
214 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
216 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
218 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
220 filehandle = _lcreat( filename, 1 ); /* readonly */
221 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
223 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
225 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
227 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
229 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
231 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
233 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
235 filehandle = _lcreat( filename, 2 );
236 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
238 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
240 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
242 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
244 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
246 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
248 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
250 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
251 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
253 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
255 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
257 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
259 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
261 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
263 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
265 filehandle=_lcreat (slashname, 0); /* illegal name */
266 if (HFILE_ERROR==filehandle) {
268 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
269 "creating file \"%s\" failed with error %d", slashname, err);
270 } else { /* only NT succeeds */
272 find=FindFirstFileA (slashname, &search_results);
273 if (INVALID_HANDLE_VALUE==find)
274 ok (0, "file \"%s\" not found", slashname);
276 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
277 slashname[strlen(slashname)-1]=0;
278 ok (!strcmp (slashname, search_results.cFileName),
279 "found unexpected name \"%s\"", search_results.cFileName);
280 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
281 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
282 search_results.dwFileAttributes);
284 ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
288 filehandle=_lcreat (filename, 8); /* illegal attribute */
289 if (HFILE_ERROR==filehandle)
290 ok (0, "couldn't create volume label \"%s\"", filename);
293 find=FindFirstFileA (filename, &search_results);
294 if (INVALID_HANDLE_VALUE==find)
295 ok (0, "file \"%s\" not found", filename);
297 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
298 ok (!strcmp (filename, search_results.cFileName),
299 "found unexpected name \"%s\"", search_results.cFileName);
300 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
302 search_results.dwFileAttributes);
304 ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
310 void test__llseek( void )
317 filehandle = _lcreat( filename, 0 );
318 if (filehandle == HFILE_ERROR)
320 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
324 for (i = 0; i < 400; i++)
326 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
328 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
329 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
331 bytes_read = _hread( filehandle, buffer, 1);
332 ok( 1 == bytes_read, "file read size error" );
333 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
334 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
336 bytes_read = _hread( filehandle, buffer, 1);
337 ok( 1 == bytes_read, "file read size error" );
338 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
339 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
340 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
342 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
346 static void test__llopen( void )
352 filehandle = _lcreat( filename, 0 );
353 if (filehandle == HFILE_ERROR)
355 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
359 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
360 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
362 filehandle = _lopen( filename, OF_READ );
363 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
364 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
365 ok( strlen( sillytext ) == bytes_read, "file read size error" );
366 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
368 filehandle = _lopen( filename, OF_READWRITE );
369 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
370 ok( strlen( sillytext ) == bytes_read, "file read size error" );
371 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
372 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
374 filehandle = _lopen( filename, OF_WRITE );
375 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
376 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
377 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
379 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
380 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
384 static void test__lread( void )
392 filehandle = _lcreat( filename, 0 );
393 if (filehandle == HFILE_ERROR)
395 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
399 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
401 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
403 filehandle = _lopen( filename, OF_READ );
405 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
407 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
409 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
411 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
413 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
414 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
415 for (i = 0; i < bytes_wanted; i++)
417 ok( buffer[i] == sillytext[i], "that's not what's written" );
421 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
423 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
427 static void test__lwrite( void )
436 HLOCAL memory_object;
439 filehandle = _lcreat( filename, 0 );
440 if (filehandle == HFILE_ERROR)
442 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
446 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
448 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
450 filehandle = _lopen( filename, OF_READ );
452 bytes_read = _hread( filehandle, buffer, 1);
454 ok( 0 == bytes_read, "file read size error" );
456 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
458 filehandle = _lopen( filename, OF_READWRITE );
462 srand( (unsigned)time( NULL ) );
463 for (blocks = 0; blocks < 100; blocks++)
465 for (i = 0; i < (long)sizeof( buffer ); i++)
468 checksum[0] = checksum[0] + buffer[i];
470 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
471 bytes_written = bytes_written + sizeof( buffer );
474 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
477 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
479 memory_object = LocalAlloc( LPTR, bytes_written );
481 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
483 contents = LocalLock( memory_object );
485 filehandle = _lopen( filename, OF_READ );
487 contents = LocalLock( memory_object );
489 ok( NULL != contents, "LocalLock whines" );
491 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
497 checksum[0] += contents[i];
500 while (i < bytes_written - 1);
502 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
504 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
506 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
509 void test_CopyFileA(void)
511 char temp_path[MAX_PATH];
512 char source[MAX_PATH], dest[MAX_PATH];
513 static const char prefix[] = "pfx";
516 ret = GetTempPathA(MAX_PATH, temp_path);
517 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
518 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
520 ret = GetTempFileNameA(temp_path, prefix, 0, source);
521 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
523 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
524 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
526 ret = CopyFileA(source, dest, TRUE);
527 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
528 "CopyFileA: unexpected error %ld\n", GetLastError());
530 ret = CopyFileA(source, dest, FALSE);
531 ok(ret, "CopyFileA: error %ld\n", GetLastError());
533 ret = DeleteFileA(source);
534 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
535 ret = DeleteFileA(dest);
536 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
539 void test_CopyFileW(void)
541 WCHAR temp_path[MAX_PATH];
542 WCHAR source[MAX_PATH], dest[MAX_PATH];
543 static const WCHAR prefix[] = {'p','f','x',0};
546 ret = GetTempPathW(MAX_PATH, temp_path);
547 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
549 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
550 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
552 ret = GetTempFileNameW(temp_path, prefix, 0, source);
553 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
555 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
556 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
558 ret = CopyFileW(source, dest, TRUE);
559 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
560 "CopyFileW: unexpected error %ld\n", GetLastError());
562 ret = CopyFileW(source, dest, FALSE);
563 ok(ret, "CopyFileW: error %ld\n", GetLastError());
565 ret = DeleteFileW(source);
566 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
567 ret = DeleteFileW(dest);
568 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
571 void test_CreateFileA(void)
574 char temp_path[MAX_PATH];
575 char filename[MAX_PATH];
576 static const char prefix[] = "pfx";
579 ret = GetTempPathA(MAX_PATH, temp_path);
580 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
581 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
583 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
584 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
586 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
587 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
588 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
589 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
591 ret = DeleteFileA(filename);
592 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
595 void test_CreateFileW(void)
598 WCHAR temp_path[MAX_PATH];
599 WCHAR filename[MAX_PATH];
600 static const WCHAR prefix[] = {'p','f','x',0};
603 ret = GetTempPathW(MAX_PATH, temp_path);
604 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
606 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
607 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
609 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
610 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
612 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
613 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
614 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
615 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
617 ret = DeleteFileW(filename);
618 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
621 static void test_DeleteFileA( void )
625 ret = DeleteFileA(NULL);
626 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
627 GetLastError() == ERROR_PATH_NOT_FOUND),
628 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
630 ret = DeleteFileA("");
631 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
632 GetLastError() == ERROR_BAD_PATHNAME),
633 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
636 static void test_DeleteFileW( void )
639 WCHAR emptyW[]={'\0'};
641 ret = DeleteFileW(NULL);
642 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
644 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
645 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
647 ret = DeleteFileW(emptyW);
648 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
649 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
652 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
654 void test_MoveFileA(void)
656 char tempdir[MAX_PATH];
657 char source[MAX_PATH], dest[MAX_PATH];
658 static const char prefix[] = "pfx";
661 ret = GetTempPathA(MAX_PATH, tempdir);
662 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
663 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
665 ret = GetTempFileNameA(tempdir, prefix, 0, source);
666 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
668 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
669 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
671 ret = MoveFileA(source, dest);
672 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
673 "MoveFileA: unexpected error %ld\n", GetLastError());
675 ret = DeleteFileA(dest);
676 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
678 ret = MoveFileA(source, dest);
679 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
681 lstrcatA(tempdir, "Remove Me");
682 ret = CreateDirectoryA(tempdir, NULL);
683 ok(ret == TRUE, "CreateDirectoryA failed");
685 lstrcpyA(source, dest);
686 lstrcpyA(dest, tempdir);
687 lstrcatA(dest, "\\wild?.*");
688 ret = MoveFileA(source, dest);
690 ok(!ret, "MoveFileA: shouldn't move to wildcard file");
691 ok(GetLastError() == ERROR_INVALID_NAME,
692 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
693 if (ret || (GetLastError() != ERROR_INVALID_NAME))
696 char temppath[MAX_PATH];
699 lstrcpyA(temppath, tempdir);
700 lstrcatA(temppath, "\\*.*");
701 hFind = FindFirstFileA(temppath, &fd);
702 if (INVALID_HANDLE_VALUE != hFind)
707 lpName = fd.cAlternateFileName;
709 lpName = fd.cFileName;
710 ok(!IsDotDir(lpName), "MoveFileA: wildcards file created!");
712 while (FindNextFileA(hFind, &fd));
718 ret = DeleteFileA(source);
719 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
720 ret = DeleteFileA(dest);
721 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
722 ret = RemoveDirectoryA(tempdir);
723 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
726 void test_MoveFileW(void)
728 WCHAR temp_path[MAX_PATH];
729 WCHAR source[MAX_PATH], dest[MAX_PATH];
730 static const WCHAR prefix[] = {'p','f','x',0};
733 ret = GetTempPathW(MAX_PATH, temp_path);
734 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
736 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
737 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
739 ret = GetTempFileNameW(temp_path, prefix, 0, source);
740 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
742 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
743 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
745 ret = MoveFileW(source, dest);
746 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
747 "CopyFileW: unexpected error %ld\n", GetLastError());
749 ret = DeleteFileW(source);
750 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
751 ret = DeleteFileW(dest);
752 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
755 #define PATTERN_OFFSET 0x10
757 void test_offset_in_overlapped_structure(void)
763 BYTE buf[256], pattern[] = "TeSt";
765 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
767 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
768 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
770 /*** Write File *****************************************************/
772 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
773 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
775 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
776 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
777 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
779 memset(&ov, 0, sizeof(ov));
780 ov.Offset = PATTERN_OFFSET;
782 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
783 /* Win 9x does not support the overlapped I/O on files */
784 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
785 ok(rc, "WriteFile error %ld", GetLastError());
786 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
787 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
788 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
789 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
791 ov.Offset = sizeof(buf) * 2;
793 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
794 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
795 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
796 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
797 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
802 /*** Read File *****************************************************/
804 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
805 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
807 memset(buf, 0, sizeof(buf));
808 memset(&ov, 0, sizeof(ov));
809 ov.Offset = PATTERN_OFFSET;
811 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
812 /* Win 9x does not support the overlapped I/O on files */
813 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
814 ok(rc, "ReadFile error %ld", GetLastError());
815 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
816 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
817 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
818 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
819 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
824 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
827 static void test_LockFile(void)
831 OVERLAPPED overlapped;
833 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
834 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
835 CREATE_ALWAYS, 0, 0 );
836 if (handle == INVALID_HANDLE_VALUE)
838 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
841 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
843 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
844 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
845 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
847 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
848 /* overlapping locks must fail */
849 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
850 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
851 /* non-overlapping locks must succeed */
852 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
854 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
855 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
856 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
857 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
859 overlapped.Offset = 100;
860 overlapped.OffsetHigh = 0;
861 overlapped.hEvent = 0;
862 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
863 /* overlapping shared locks are OK */
864 overlapped.Offset = 150;
865 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
866 /* but exclusive is not */
867 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
868 "LockFileEx exclusive 150,50 succeeded" );
869 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
870 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
871 overlapped.Offset = 100;
872 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
873 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
875 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
876 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
877 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
878 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
880 /* wrap-around lock should not do anything */
881 /* (but still succeeds on NT4 so we don't check result) */
882 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
883 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
884 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
887 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
888 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
889 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
890 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
891 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
892 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
893 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
895 CloseHandle( handle );
896 DeleteFileA( filename );
899 void test_FindFirstFileA()
902 WIN32_FIND_DATAA search_results;
905 handle = FindFirstFileA("C:\\",&search_results);
906 err = GetLastError();
907 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
908 if (handle == INVALID_HANDLE_VALUE)
909 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
910 handle = FindFirstFileA("C:\\*",&search_results);
911 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
912 ok ( FindClose(handle) == TRUE, "Failed to close handle");
915 void test_FindNextFileA()
918 WIN32_FIND_DATAA search_results;
921 handle = FindFirstFileA("C:\\*",&search_results);
922 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
923 while (FindNextFile(handle, &search_results))
925 /* get to the end of the files */
927 ok ( FindClose(handle) == TRUE, "Failed to close handle");
928 err = GetLastError();
929 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES");
948 test_FindFirstFileA();
949 test_FindNextFileA();
951 test_offset_in_overlapped_structure();