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 static 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 static 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 static 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 static 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 static 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());
622 static void test_GetTempFileNameA() {
625 char *expected = "c:\\windows\\abc2.tmp";
627 /* this test may depend on the config file settings */
628 result = GetTempFileNameA("C:", "abc", 1, out);
629 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
630 ok( ((out[0] == 'C') && (out[1] == ':')) && (out[2] == '\\'), "GetTempFileNameA: first three characters should be C:\\, string was actually %s", out );
632 result = GetTempFileNameA("c:\\windows\\", "abc", 2, out);
633 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
634 ok( strcasecmp( out, expected ) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n", out, expected );
638 static void test_DeleteFileA( void )
642 ret = DeleteFileA(NULL);
643 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
644 GetLastError() == ERROR_PATH_NOT_FOUND),
645 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
647 ret = DeleteFileA("");
648 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
649 GetLastError() == ERROR_BAD_PATHNAME),
650 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
653 static void test_DeleteFileW( void )
656 WCHAR emptyW[]={'\0'};
658 ret = DeleteFileW(NULL);
659 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
661 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
662 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
664 ret = DeleteFileW(emptyW);
665 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
666 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
669 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
671 static void test_MoveFileA(void)
673 char tempdir[MAX_PATH];
674 char source[MAX_PATH], dest[MAX_PATH];
675 static const char prefix[] = "pfx";
678 ret = GetTempPathA(MAX_PATH, tempdir);
679 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
680 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
682 ret = GetTempFileNameA(tempdir, prefix, 0, source);
683 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
685 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
686 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
688 ret = MoveFileA(source, dest);
689 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
690 "MoveFileA: unexpected error %ld\n", GetLastError());
692 ret = DeleteFileA(dest);
693 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
695 ret = MoveFileA(source, dest);
696 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
698 lstrcatA(tempdir, "Remove Me");
699 ret = CreateDirectoryA(tempdir, NULL);
700 ok(ret == TRUE, "CreateDirectoryA failed");
702 lstrcpyA(source, dest);
703 lstrcpyA(dest, tempdir);
704 lstrcatA(dest, "\\wild?.*");
705 ret = MoveFileA(source, dest);
707 ok(!ret, "MoveFileA: shouldn't move to wildcard file");
708 ok(GetLastError() == ERROR_INVALID_NAME,
709 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
711 if (ret || (GetLastError() != ERROR_INVALID_NAME))
714 char temppath[MAX_PATH];
717 lstrcpyA(temppath, tempdir);
718 lstrcatA(temppath, "\\*.*");
719 hFind = FindFirstFileA(temppath, &fd);
720 if (INVALID_HANDLE_VALUE != hFind)
725 lpName = fd.cAlternateFileName;
727 lpName = fd.cFileName;
728 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!");
730 while (FindNextFileA(hFind, &fd));
735 ret = DeleteFileA(source);
736 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
737 ret = DeleteFileA(dest);
738 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
741 ret = RemoveDirectoryA(tempdir);
742 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
745 static void test_MoveFileW(void)
747 WCHAR temp_path[MAX_PATH];
748 WCHAR source[MAX_PATH], dest[MAX_PATH];
749 static const WCHAR prefix[] = {'p','f','x',0};
752 ret = GetTempPathW(MAX_PATH, temp_path);
753 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
755 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
756 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
758 ret = GetTempFileNameW(temp_path, prefix, 0, source);
759 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
761 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
762 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
764 ret = MoveFileW(source, dest);
765 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
766 "CopyFileW: unexpected error %ld\n", GetLastError());
768 ret = DeleteFileW(source);
769 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
770 ret = DeleteFileW(dest);
771 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
774 #define PATTERN_OFFSET 0x10
776 static void test_offset_in_overlapped_structure(void)
782 BYTE buf[256], pattern[] = "TeSt";
784 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
786 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
787 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
789 /*** Write File *****************************************************/
791 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
792 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
794 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
795 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
796 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
798 memset(&ov, 0, sizeof(ov));
799 ov.Offset = PATTERN_OFFSET;
801 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
802 /* Win 9x does not support the overlapped I/O on files */
803 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
804 ok(rc, "WriteFile error %ld", GetLastError());
805 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
806 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
807 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
808 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
810 ov.Offset = sizeof(buf) * 2;
812 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
813 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
814 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
815 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
816 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
821 /*** Read File *****************************************************/
823 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
824 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
826 memset(buf, 0, sizeof(buf));
827 memset(&ov, 0, sizeof(ov));
828 ov.Offset = PATTERN_OFFSET;
830 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
831 /* Win 9x does not support the overlapped I/O on files */
832 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
833 ok(rc, "ReadFile error %ld", GetLastError());
834 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
835 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
836 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
837 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
838 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
843 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
846 static void test_LockFile(void)
850 OVERLAPPED overlapped;
852 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
853 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
854 CREATE_ALWAYS, 0, 0 );
855 if (handle == INVALID_HANDLE_VALUE)
857 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
860 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
862 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
863 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
864 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
866 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
867 /* overlapping locks must fail */
868 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
869 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
870 /* non-overlapping locks must succeed */
871 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
873 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
874 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
875 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
876 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
878 overlapped.Offset = 100;
879 overlapped.OffsetHigh = 0;
880 overlapped.hEvent = 0;
881 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
882 /* overlapping shared locks are OK */
883 overlapped.Offset = 150;
884 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
885 /* but exclusive is not */
886 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
887 "LockFileEx exclusive 150,50 succeeded" );
888 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
889 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
890 overlapped.Offset = 100;
891 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
892 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
894 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
895 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
896 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
897 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
899 /* wrap-around lock should not do anything */
900 /* (but still succeeds on NT4 so we don't check result) */
901 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
902 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
903 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
906 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
907 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
908 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
909 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
910 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
911 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
912 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
914 CloseHandle( handle );
915 DeleteFileA( filename );
918 static void test_FindFirstFileA()
921 WIN32_FIND_DATAA search_results;
924 handle = FindFirstFileA("C:\\",&search_results);
925 err = GetLastError();
926 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
927 if (handle == INVALID_HANDLE_VALUE)
928 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
929 handle = FindFirstFileA("C:\\*",&search_results);
930 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
931 ok ( FindClose(handle) == TRUE, "Failed to close handle");
934 static void test_FindNextFileA()
937 WIN32_FIND_DATAA search_results;
940 handle = FindFirstFileA("C:\\*",&search_results);
941 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
942 while (FindNextFile(handle, &search_results))
944 /* get to the end of the files */
946 ok ( FindClose(handle) == TRUE, "Failed to close handle");
947 err = GetLastError();
948 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES");
951 static void test_MapFile()
955 /* be sure to remove stale files */
956 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
957 DeleteFile(filename);
958 handle = CreateFile( filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
959 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
960 ok( handle != INVALID_HANDLE_VALUE, "couldn't create test file\n");
962 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
963 ok( hmap == NULL, "mapped zero size file\n");
964 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
966 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
967 ok( hmap == NULL, "mapping should fail\n");
968 /* GetLastError() varies between win9x and WinNT */
970 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
971 ok( hmap == NULL, "mapping should fail\n");
972 /* GetLastError() varies between win9x and WinNT */
974 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, NULL );
975 ok( hmap != NULL, "mapping should succeed\n");
977 ok( CloseHandle( hmap ), "can't close mapping handle\n");
978 ok( CloseHandle( handle ), "can't close file handle\n");
979 ok( DeleteFileA( filename ), "DeleteFile failed after map" );
992 test_GetTempFileNameA();
1001 test_FindFirstFileA();
1002 test_FindNextFileA();
1004 test_offset_in_overlapped_structure();