Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / kernel / tests / file.c
blobd1fb8eb0c15765dfe3c2a651eb57e95fe89ae4d0
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 <stdarg.h>
23 #include <stdlib.h>
24 #include <time.h>
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
32 LPCSTR filename = "testfile.xxx";
33 LPCSTR sillytext =
34 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
43 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
46 static void test__hread( void )
48 HFILE filehandle;
49 char buffer[10000];
50 long bytes_read;
51 long bytes_wanted;
52 long i;
54 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
55 DeleteFileA( filename );
56 filehandle = _lcreat( filename, 0 );
57 if (filehandle == HFILE_ERROR)
59 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
60 return;
63 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
65 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
67 filehandle = _lopen( filename, OF_READ );
69 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError( ) );
71 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
73 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
75 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
77 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
78 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
79 for (i = 0; i < bytes_wanted; i++)
81 ok( buffer[i] == sillytext[i], "that's not what's written" );
85 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
87 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
91 static void test__hwrite( void )
93 HFILE filehandle;
94 char buffer[10000];
95 long bytes_read;
96 long bytes_written;
97 long blocks;
98 long i;
99 char *contents;
100 HLOCAL memory_object;
101 char checksum[1];
103 filehandle = _lcreat( filename, 0 );
104 if (filehandle == HFILE_ERROR)
106 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
107 return;
110 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
112 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
114 filehandle = _lopen( filename, OF_READ );
116 bytes_read = _hread( filehandle, buffer, 1);
118 ok( 0 == bytes_read, "file read size error" );
120 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
122 filehandle = _lopen( filename, OF_READWRITE );
124 bytes_written = 0;
125 checksum[0] = '\0';
126 srand( (unsigned)time( NULL ) );
127 for (blocks = 0; blocks < 100; blocks++)
129 for (i = 0; i < (long)sizeof( buffer ); i++)
131 buffer[i] = rand( );
132 checksum[0] = checksum[0] + buffer[i];
134 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
135 bytes_written = bytes_written + sizeof( buffer );
138 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
139 bytes_written++;
141 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
143 memory_object = LocalAlloc( LPTR, bytes_written );
145 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
147 contents = LocalLock( memory_object );
149 filehandle = _lopen( filename, OF_READ );
151 contents = LocalLock( memory_object );
153 ok( NULL != contents, "LocalLock whines" );
155 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
157 checksum[0] = '\0';
158 i = 0;
161 checksum[0] = checksum[0] + contents[i];
162 i++;
164 while (i < bytes_written - 1);
166 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
168 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
170 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
174 static void test__lclose( void )
176 HFILE filehandle;
178 filehandle = _lcreat( filename, 0 );
179 if (filehandle == HFILE_ERROR)
181 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
182 return;
185 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
187 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
189 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
193 static void test__lcreat( void )
195 HFILE filehandle;
196 char buffer[10000];
197 WIN32_FIND_DATAA search_results;
198 char slashname[] = "testfi/";
199 HANDLE find;
201 filehandle = _lcreat( filename, 0 );
202 if (filehandle == HFILE_ERROR)
204 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
205 return;
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)
267 ok (0, "couldn't create file \"%s\" (err=%ld)", slashname,
268 GetLastError ());
269 else {
270 _lclose(filehandle);
271 find=FindFirstFileA (slashname, &search_results);
272 if (INVALID_HANDLE_VALUE==find)
273 ok (0, "file \"%s\" not found", slashname);
274 else {
275 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
276 slashname[strlen(slashname)-1]=0;
277 ok (!strcmp (slashname, search_results.cFileName),
278 "found unexpected name \"%s\"", search_results.cFileName);
279 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
280 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
281 search_results.dwFileAttributes);
283 ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
284 GetLastError ());
287 filehandle=_lcreat (filename, 8); /* illegal attribute */
288 if (HFILE_ERROR==filehandle)
289 ok (0, "couldn't create volume label \"%s\"", filename);
290 else {
291 _lclose(filehandle);
292 find=FindFirstFileA (filename, &search_results);
293 if (INVALID_HANDLE_VALUE==find)
294 ok (0, "file \"%s\" not found", filename);
295 else {
296 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
297 ok (!strcmp (filename, search_results.cFileName),
298 "found unexpected name \"%s\"", search_results.cFileName);
299 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
300 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
301 search_results.dwFileAttributes);
303 ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
304 GetLastError ());
309 void test__llseek( void )
311 INT i;
312 HFILE filehandle;
313 char buffer[1];
314 long bytes_read;
316 filehandle = _lcreat( filename, 0 );
317 if (filehandle == HFILE_ERROR)
319 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
320 return;
323 for (i = 0; i < 400; i++)
325 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
327 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
328 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
330 bytes_read = _hread( filehandle, buffer, 1);
331 ok( 1 == bytes_read, "file read size error" );
332 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
333 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
335 bytes_read = _hread( filehandle, buffer, 1);
336 ok( 1 == bytes_read, "file read size error" );
337 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
338 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
339 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
341 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
345 static void test__llopen( void )
347 HFILE filehandle;
348 UINT bytes_read;
349 char buffer[10000];
351 filehandle = _lcreat( filename, 0 );
352 if (filehandle == HFILE_ERROR)
354 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
355 return;
358 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
359 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
361 filehandle = _lopen( filename, OF_READ );
362 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
363 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
364 ok( strlen( sillytext ) == bytes_read, "file read size error" );
365 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
367 filehandle = _lopen( filename, OF_READWRITE );
368 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
369 ok( strlen( sillytext ) == bytes_read, "file read size error" );
370 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
371 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
373 filehandle = _lopen( filename, OF_WRITE );
374 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
375 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
376 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
378 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
379 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
383 static void test__lread( void )
385 HFILE filehandle;
386 char buffer[10000];
387 long bytes_read;
388 UINT bytes_wanted;
389 UINT i;
391 filehandle = _lcreat( filename, 0 );
392 if (filehandle == HFILE_ERROR)
394 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
395 return;
398 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
400 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
402 filehandle = _lopen( filename, OF_READ );
404 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
406 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
408 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
410 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
412 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
413 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
414 for (i = 0; i < bytes_wanted; i++)
416 ok( buffer[i] == sillytext[i], "that's not what's written" );
420 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
422 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
426 static void test__lwrite( void )
428 HFILE filehandle;
429 char buffer[10000];
430 long bytes_read;
431 long bytes_written;
432 long blocks;
433 long i;
434 char *contents;
435 HLOCAL memory_object;
436 char checksum[1];
438 filehandle = _lcreat( filename, 0 );
439 if (filehandle == HFILE_ERROR)
441 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
442 return;
445 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
447 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
449 filehandle = _lopen( filename, OF_READ );
451 bytes_read = _hread( filehandle, buffer, 1);
453 ok( 0 == bytes_read, "file read size error" );
455 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
457 filehandle = _lopen( filename, OF_READWRITE );
459 bytes_written = 0;
460 checksum[0] = '\0';
461 srand( (unsigned)time( NULL ) );
462 for (blocks = 0; blocks < 100; blocks++)
464 for (i = 0; i < (long)sizeof( buffer ); i++)
466 buffer[i] = rand( );
467 checksum[0] = checksum[0] + buffer[i];
469 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
470 bytes_written = bytes_written + sizeof( buffer );
473 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
474 bytes_written++;
476 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
478 memory_object = LocalAlloc( LPTR, bytes_written );
480 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
482 contents = LocalLock( memory_object );
484 filehandle = _lopen( filename, OF_READ );
486 contents = LocalLock( memory_object );
488 ok( NULL != contents, "LocalLock whines" );
490 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
492 checksum[0] = '\0';
493 i = 0;
496 checksum[0] += contents[i];
497 i++;
499 while (i < bytes_written - 1);
501 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
503 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
505 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
508 void test_CopyFileA(void)
510 char temp_path[MAX_PATH];
511 char source[MAX_PATH], dest[MAX_PATH];
512 static const char prefix[] = "pfx";
513 DWORD ret;
515 ret = GetTempPathA(MAX_PATH, temp_path);
516 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
517 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
519 ret = GetTempFileNameA(temp_path, prefix, 0, source);
520 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
522 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
523 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
525 ret = CopyFileA(source, dest, TRUE);
526 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
527 "CopyFileA: unexpected error %ld\n", GetLastError());
529 ret = CopyFileA(source, dest, FALSE);
530 ok(ret, "CopyFileA: error %ld\n", GetLastError());
532 ret = DeleteFileA(source);
533 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
534 ret = DeleteFileA(dest);
535 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
538 void test_CopyFileW(void)
540 WCHAR temp_path[MAX_PATH];
541 WCHAR source[MAX_PATH], dest[MAX_PATH];
542 static const WCHAR prefix[] = {'p','f','x',0};
543 DWORD ret;
545 ret = GetTempPathW(MAX_PATH, temp_path);
546 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
547 return;
548 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
549 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
551 ret = GetTempFileNameW(temp_path, prefix, 0, source);
552 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
554 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
555 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
557 ret = CopyFileW(source, dest, TRUE);
558 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
559 "CopyFileW: unexpected error %ld\n", GetLastError());
561 ret = CopyFileW(source, dest, FALSE);
562 ok(ret, "CopyFileW: error %ld\n", GetLastError());
564 ret = DeleteFileW(source);
565 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
566 ret = DeleteFileW(dest);
567 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
570 void test_CreateFileA(void)
572 HANDLE hFile;
573 char temp_path[MAX_PATH];
574 char filename[MAX_PATH];
575 static const char prefix[] = "pfx";
576 DWORD ret;
578 ret = GetTempPathA(MAX_PATH, temp_path);
579 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
580 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
582 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
583 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
585 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
586 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
587 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
588 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
590 ret = DeleteFileA(filename);
591 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
594 void test_CreateFileW(void)
596 HANDLE hFile;
597 WCHAR temp_path[MAX_PATH];
598 WCHAR filename[MAX_PATH];
599 static const WCHAR prefix[] = {'p','f','x',0};
600 DWORD ret;
602 ret = GetTempPathW(MAX_PATH, temp_path);
603 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
604 return;
605 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
606 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
608 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
609 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
611 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
612 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
613 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
614 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
616 ret = DeleteFileW(filename);
617 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
620 static void test_DeleteFileA( void )
622 BOOL ret;
624 ret = DeleteFileA(NULL);
625 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
626 GetLastError() == ERROR_PATH_NOT_FOUND),
627 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
629 ret = DeleteFileA("");
630 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
631 GetLastError() == ERROR_BAD_PATHNAME),
632 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
635 static void test_DeleteFileW( void )
637 BOOL ret;
638 WCHAR emptyW[]={'\0'};
640 ret = DeleteFileW(NULL);
641 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
642 return;
643 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
644 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
646 ret = DeleteFileW(emptyW);
647 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
648 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
651 #define PATTERN_OFFSET 0x10
653 void test_offset_in_overlapped_structure(void)
655 HANDLE hFile;
656 OVERLAPPED ov;
657 DWORD done;
658 BOOL rc;
659 BYTE buf[256], pattern[] = "TeSt";
660 UINT i;
661 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
663 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
664 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
666 /*** Write File *****************************************************/
668 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
669 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
671 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
672 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
673 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
675 memset(&ov, 0, sizeof(ov));
676 ov.Offset = PATTERN_OFFSET;
677 ov.OffsetHigh = 0;
678 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
679 /* Win 9x does not support the overlapped I/O on files */
680 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
681 ok(rc, "WriteFile error %ld", GetLastError());
682 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
683 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
684 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
685 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
687 ov.Offset = sizeof(buf) * 2;
688 ov.OffsetHigh = 0;
689 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
690 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
691 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
692 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
693 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
696 CloseHandle(hFile);
698 /*** Read File *****************************************************/
700 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
701 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
703 memset(buf, 0, sizeof(buf));
704 memset(&ov, 0, sizeof(ov));
705 ov.Offset = PATTERN_OFFSET;
706 ov.OffsetHigh = 0;
707 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
708 /* Win 9x does not support the overlapped I/O on files */
709 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
710 ok(rc, "ReadFile error %ld", GetLastError());
711 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
712 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
713 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
714 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
715 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
718 CloseHandle(hFile);
720 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
723 static void test_LockFile(void)
725 HANDLE handle;
726 DWORD written;
727 OVERLAPPED overlapped;
729 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
730 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
731 CREATE_ALWAYS, 0, 0 );
732 if (handle == INVALID_HANDLE_VALUE)
734 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
735 return;
737 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
739 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
740 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
741 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
743 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
744 /* overlapping locks must fail */
745 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
746 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
747 /* non-overlapping locks must succeed */
748 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
750 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
751 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
752 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
753 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
755 overlapped.Offset = 100;
756 overlapped.OffsetHigh = 0;
757 overlapped.hEvent = 0;
758 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
759 /* overlapping shared locks are OK */
760 overlapped.Offset = 150;
761 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
762 /* but exclusive is not */
763 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
764 "LockFileEx exclusive 150,50 succeeded" );
765 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
766 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
767 overlapped.Offset = 100;
768 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
769 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
771 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
772 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
773 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
774 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
776 /* wrap-around lock should not do anything */
777 /* (but still succeeds on NT4 so we don't check result) */
778 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
779 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
780 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
782 /* zero-byte lock */
783 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
784 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
785 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
786 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
787 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
788 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
789 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
791 CloseHandle( handle );
792 DeleteFileA( filename );
795 void test_FindFirstFileA()
797 HANDLE handle;
798 WIN32_FIND_DATAA search_results;
799 int err;
801 handle = FindFirstFileA("C:",&search_results);
802 err = GetLastError();
803 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
804 if (handle == INVALID_HANDLE_VALUE)
805 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
806 handle = FindFirstFileA("C:\\",&search_results);
807 err = GetLastError();
808 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
809 if (handle == INVALID_HANDLE_VALUE)
810 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
811 handle = FindFirstFileA("C:\\*",&search_results);
812 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
813 ok ( FindClose(handle) == TRUE, "Failed to close handle");
816 START_TEST(file)
818 test__hread( );
819 test__hwrite( );
820 test__lclose( );
821 test__lcreat( );
822 test__llseek( );
823 test__llopen( );
824 test__lread( );
825 test__lwrite( );
826 test_CopyFileA();
827 test_CopyFileW();
828 test_CreateFileA();
829 test_CreateFileW();
830 test_DeleteFileA();
831 test_DeleteFileW();
832 test_FindFirstFileA();
833 test_LockFile();
834 test_offset_in_overlapped_structure();