FindFirstFile on root directory should fail.
[wine/wine64.git] / dlls / kernel / tests / file.c
blob0ff985fb9765f55c4b906db4c27329364fd665b8
1 /*
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
22 #include <stdlib.h>
23 #include <time.h>
25 #include "wine/test.h"
26 #include "winbase.h"
27 #include "winerror.h"
30 LPCSTR filename = "testfile.xxx";
31 LPCSTR sillytext =
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 )
46 HFILE filehandle;
47 char buffer[10000];
48 long bytes_read;
49 long bytes_wanted;
50 long i;
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());
58 return;
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 )
91 HFILE filehandle;
92 char buffer[10000];
93 long bytes_read;
94 long bytes_written;
95 long blocks;
96 long i;
97 char *contents;
98 HLOCAL memory_object;
99 char checksum[1];
101 filehandle = _lcreat( filename, 0 );
102 if (filehandle == HFILE_ERROR)
104 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
105 return;
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 );
122 bytes_written = 0;
123 checksum[0] = '\0';
124 srand( (unsigned)time( NULL ) );
125 for (blocks = 0; blocks < 100; blocks++)
127 for (i = 0; i < sizeof( buffer ); i++)
129 buffer[i] = rand( );
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" );
137 bytes_written++;
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" );
155 checksum[0] = '\0';
156 i = 0;
159 checksum[0] = checksum[0] + contents[i];
160 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 )
174 HFILE filehandle;
176 filehandle = _lcreat( filename, 0 );
177 if (filehandle == HFILE_ERROR)
179 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
180 return;
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 )
193 HFILE filehandle;
194 char buffer[10000];
195 WIN32_FIND_DATAA search_results;
197 filehandle = _lcreat( filename, 0 );
198 if (filehandle == HFILE_ERROR)
200 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
201 return;
204 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
206 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
208 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
210 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
212 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
214 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
216 filehandle = _lcreat( filename, 1 ); /* readonly */
217 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
219 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
221 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
223 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
225 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
227 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
229 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
231 filehandle = _lcreat( filename, 2 );
232 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
234 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
236 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
238 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
240 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
242 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
244 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
246 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
247 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
249 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
251 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
253 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
255 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
257 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
259 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
263 void test__llseek( void )
265 INT i;
266 HFILE filehandle;
267 char buffer[1];
268 long bytes_read;
270 filehandle = _lcreat( filename, 0 );
271 if (filehandle == HFILE_ERROR)
273 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
274 return;
277 for (i = 0; i < 400; i++)
279 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
281 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
282 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
284 bytes_read = _hread( filehandle, buffer, 1);
285 ok( 1 == bytes_read, "file read size error" );
286 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
287 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
289 bytes_read = _hread( filehandle, buffer, 1);
290 ok( 1 == bytes_read, "file read size error" );
291 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
292 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
293 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
295 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
299 static void test__llopen( void )
301 HFILE filehandle;
302 UINT bytes_read;
303 char buffer[10000];
305 filehandle = _lcreat( filename, 0 );
306 if (filehandle == HFILE_ERROR)
308 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
309 return;
312 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
313 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
315 filehandle = _lopen( filename, OF_READ );
316 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
317 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
318 ok( strlen( sillytext ) == bytes_read, "file read size error" );
319 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
321 filehandle = _lopen( filename, OF_READWRITE );
322 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
323 ok( strlen( sillytext ) == bytes_read, "file read size error" );
324 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
325 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
327 filehandle = _lopen( filename, OF_WRITE );
328 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
329 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
330 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
332 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
333 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
337 static void test__lread( void )
339 HFILE filehandle;
340 char buffer[10000];
341 long bytes_read;
342 UINT bytes_wanted;
343 UINT i;
345 filehandle = _lcreat( filename, 0 );
346 if (filehandle == HFILE_ERROR)
348 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
349 return;
352 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
354 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
356 filehandle = _lopen( filename, OF_READ );
358 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
360 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
362 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
364 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
366 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
367 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
368 for (i = 0; i < bytes_wanted; i++)
370 ok( buffer[i] == sillytext[i], "that's not what's written" );
374 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
376 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
380 static void test__lwrite( void )
382 HFILE filehandle;
383 char buffer[10000];
384 long bytes_read;
385 long bytes_written;
386 long blocks;
387 long i;
388 char *contents;
389 HLOCAL memory_object;
390 char checksum[1];
392 filehandle = _lcreat( filename, 0 );
393 if (filehandle == HFILE_ERROR)
395 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
396 return;
399 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
401 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
403 filehandle = _lopen( filename, OF_READ );
405 bytes_read = _hread( filehandle, buffer, 1);
407 ok( 0 == bytes_read, "file read size error" );
409 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
411 filehandle = _lopen( filename, OF_READWRITE );
413 bytes_written = 0;
414 checksum[0] = '\0';
415 srand( (unsigned)time( NULL ) );
416 for (blocks = 0; blocks < 100; blocks++)
418 for (i = 0; i < sizeof( buffer ); i++)
420 buffer[i] = rand( );
421 checksum[0] = checksum[0] + buffer[i];
423 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
424 bytes_written = bytes_written + sizeof( buffer );
427 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
428 bytes_written++;
430 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
432 memory_object = LocalAlloc( LPTR, bytes_written );
434 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
436 contents = LocalLock( memory_object );
438 filehandle = _lopen( filename, OF_READ );
440 contents = LocalLock( memory_object );
442 ok( NULL != contents, "LocalLock whines" );
444 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
446 checksum[0] = '\0';
447 i = 0;
450 checksum[0] += contents[i];
451 i++;
453 while (i < bytes_written - 1);
455 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
457 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
459 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
462 void test_CopyFileA(void)
464 char temp_path[MAX_PATH];
465 char source[MAX_PATH], dest[MAX_PATH];
466 static const char prefix[] = "pfx";
467 DWORD ret;
469 ret = GetTempPathA(MAX_PATH, temp_path);
470 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
471 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
473 ret = GetTempFileNameA(temp_path, prefix, 0, source);
474 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
476 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
477 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
479 ret = CopyFileA(source, dest, TRUE);
480 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
481 "CopyFileA: unexpected error %ld\n", GetLastError());
483 ret = CopyFileA(source, dest, FALSE);
484 ok(ret, "CopyFileA: error %ld\n", GetLastError());
486 ret = DeleteFileA(source);
487 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
488 ret = DeleteFileA(dest);
489 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
492 void test_CopyFileW(void)
494 WCHAR temp_path[MAX_PATH];
495 WCHAR source[MAX_PATH], dest[MAX_PATH];
496 static const WCHAR prefix[] = {'p','f','x',0};
497 DWORD ret;
499 ret = GetTempPathW(MAX_PATH, temp_path);
500 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
501 return;
502 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
503 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
505 ret = GetTempFileNameW(temp_path, prefix, 0, source);
506 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
508 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
509 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
511 ret = CopyFileW(source, dest, TRUE);
512 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
513 "CopyFileW: unexpected error %ld\n", GetLastError());
515 ret = CopyFileW(source, dest, FALSE);
516 ok(ret, "CopyFileW: error %ld\n", GetLastError());
518 ret = DeleteFileW(source);
519 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
520 ret = DeleteFileW(dest);
521 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
524 void test_CreateFileA(void)
526 HANDLE hFile;
527 char temp_path[MAX_PATH];
528 char filename[MAX_PATH];
529 static const char prefix[] = "pfx";
530 DWORD ret;
532 ret = GetTempPathA(MAX_PATH, temp_path);
533 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
534 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
536 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
537 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
539 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
540 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
541 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
542 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
544 ret = DeleteFileA(filename);
545 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
548 void test_CreateFileW(void)
550 HANDLE hFile;
551 WCHAR temp_path[MAX_PATH];
552 WCHAR filename[MAX_PATH];
553 static const WCHAR prefix[] = {'p','f','x',0};
554 DWORD ret;
556 ret = GetTempPathW(MAX_PATH, temp_path);
557 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
558 return;
559 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
560 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
562 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
563 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
565 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
566 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
567 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
568 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
570 ret = DeleteFileW(filename);
571 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
574 static void test_DeleteFileA( void )
576 BOOL ret;
578 ret = DeleteFileA(NULL);
579 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
580 GetLastError() == ERROR_PATH_NOT_FOUND),
581 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
583 ret = DeleteFileA("");
584 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
585 GetLastError() == ERROR_BAD_PATHNAME),
586 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
589 static void test_DeleteFileW( void )
591 BOOL ret;
592 WCHAR emptyW[]={'\0'};
594 ret = DeleteFileW(NULL);
595 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
596 return;
597 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
598 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
600 ret = DeleteFileW(emptyW);
601 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
602 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
605 #define PATTERN_OFFSET 0x10
607 void test_offset_in_overlapped_structure(void)
609 HANDLE hFile;
610 OVERLAPPED ov;
611 DWORD done;
612 BOOL rc;
613 BYTE buf[256], pattern[] = "TeSt";
614 UINT i;
615 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
617 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
618 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
620 /*** Write File *****************************************************/
622 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
623 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
625 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
626 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
627 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
629 memset(&ov, 0, sizeof(ov));
630 ov.Offset = PATTERN_OFFSET;
631 ov.OffsetHigh = 0;
632 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
633 /* Win 9x does not support the overlapped I/O on files */
634 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
635 ok(rc, "WriteFile error %ld", GetLastError());
636 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
637 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
638 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
639 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
641 ov.Offset = sizeof(buf) * 2;
642 ov.OffsetHigh = 0;
643 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
644 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
645 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
646 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
647 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
650 CloseHandle(hFile);
652 /*** Read File *****************************************************/
654 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
655 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
657 memset(buf, 0, sizeof(buf));
658 memset(&ov, 0, sizeof(ov));
659 ov.Offset = PATTERN_OFFSET;
660 ov.OffsetHigh = 0;
661 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
662 /* Win 9x does not support the overlapped I/O on files */
663 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
664 ok(rc, "ReadFile error %ld", GetLastError());
665 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
666 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
667 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
668 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
669 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
672 CloseHandle(hFile);
674 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
677 static void test_LockFile(void)
679 HANDLE handle;
680 DWORD written;
681 OVERLAPPED overlapped;
683 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
684 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
685 CREATE_ALWAYS, 0, 0 );
686 if (handle == INVALID_HANDLE_VALUE)
688 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
689 return;
691 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
693 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
694 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
695 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
697 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
698 /* overlapping locks must fail */
699 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
700 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
701 /* non-overlapping locks must succeed */
702 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
704 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
705 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
706 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
707 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
709 overlapped.Offset = 100;
710 overlapped.OffsetHigh = 0;
711 overlapped.hEvent = 0;
712 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
713 /* overlapping shared locks are OK */
714 overlapped.Offset = 150;
715 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
716 /* but exclusive is not */
717 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
718 "LockFileEx exclusive 150,50 succeeded" );
719 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
720 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
721 overlapped.Offset = 100;
722 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
723 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
725 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
726 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
727 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
728 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
730 /* wrap-around lock should not do anything */
731 /* (but still succeeds on NT4 so we don't check result) */
732 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
733 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
734 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
736 /* zero-byte lock */
737 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
738 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
739 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
740 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
741 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
742 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
743 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
745 CloseHandle( handle );
746 DeleteFileA( filename );
749 void test_FindFirstFileA()
751 HANDLE handle;
752 WIN32_FIND_DATAA search_results;
753 int err;
755 handle = FindFirstFileA("C:",&search_results);
756 err = GetLastError();
757 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
758 if (handle == INVALID_HANDLE_VALUE)
759 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
760 handle = FindFirstFileA("C:\\",&search_results);
761 err = GetLastError();
762 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
763 if (handle == INVALID_HANDLE_VALUE)
764 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
765 handle = FindFirstFileA("C:\\*",&search_results);
766 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
767 ok ( FindClose(handle) == TRUE, "Failed to close handle");
770 START_TEST(file)
772 test__hread( );
773 test__hwrite( );
774 test__lclose( );
775 test__lcreat( );
776 test__llseek( );
777 test__llopen( );
778 test__lread( );
779 test__lwrite( );
780 test_CopyFileA();
781 test_CopyFileW();
782 test_CreateFileA();
783 test_CreateFileW();
784 test_DeleteFileA();
785 test_DeleteFileW();
786 test_FindFirstFileA();
787 test_LockFile();
788 test_offset_in_overlapped_structure();