Check file sharing permissions based on the file inode instead of the
[wine/wine64.git] / dlls / kernel / tests / file.c
blobf7341061b9a397de350c712368d6ec8b7285e8c5
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"
31 LPCSTR filename = "testfile.xxx";
32 LPCSTR sillytext =
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 )
47 HFILE filehandle;
48 char buffer[10000];
49 long bytes_read;
50 long bytes_wanted;
51 long i;
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)\n",filename,GetLastError());
59 return;
62 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
64 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
66 filehandle = _lopen( filename, OF_READ );
68 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError( ) );
70 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
72 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
74 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
76 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
77 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
78 for (i = 0; i < bytes_wanted; i++)
80 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
84 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
86 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
90 static void test__hwrite( void )
92 HFILE filehandle;
93 char buffer[10000];
94 long bytes_read;
95 long bytes_written;
96 long blocks;
97 long i;
98 char *contents;
99 HLOCAL memory_object;
100 char checksum[1];
102 filehandle = _lcreat( filename, 0 );
103 if (filehandle == HFILE_ERROR)
105 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
106 return;
109 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
111 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
113 filehandle = _lopen( filename, OF_READ );
115 bytes_read = _hread( filehandle, buffer, 1);
117 ok( 0 == bytes_read, "file read size error\n" );
119 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
121 filehandle = _lopen( filename, OF_READWRITE );
123 bytes_written = 0;
124 checksum[0] = '\0';
125 srand( (unsigned)time( NULL ) );
126 for (blocks = 0; blocks < 100; blocks++)
128 for (i = 0; i < (long)sizeof( buffer ); i++)
130 buffer[i] = rand( );
131 checksum[0] = checksum[0] + buffer[i];
133 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
134 bytes_written = bytes_written + sizeof( buffer );
137 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
138 bytes_written++;
140 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
142 memory_object = LocalAlloc( LPTR, bytes_written );
144 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
146 contents = LocalLock( memory_object );
148 filehandle = _lopen( filename, OF_READ );
150 contents = LocalLock( memory_object );
152 ok( NULL != contents, "LocalLock whines\n" );
154 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
156 checksum[0] = '\0';
157 i = 0;
160 checksum[0] = checksum[0] + contents[i];
161 i++;
163 while (i < bytes_written - 1);
165 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
167 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
169 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
173 static void test__lclose( void )
175 HFILE filehandle;
177 filehandle = _lcreat( filename, 0 );
178 if (filehandle == HFILE_ERROR)
180 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
181 return;
184 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
186 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
188 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
192 static void test__lcreat( void )
194 HFILE filehandle;
195 char buffer[10000];
196 WIN32_FIND_DATAA search_results;
197 char slashname[] = "testfi/";
198 int err;
199 HANDLE find;
201 filehandle = _lcreat( filename, 0 );
202 if (filehandle == HFILE_ERROR)
204 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
205 return;
208 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
210 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
212 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
214 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
216 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
218 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)\n", GetLastError());
220 filehandle = _lcreat( filename, 1 ); /* readonly */
221 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
223 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
225 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
227 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
229 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
231 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
233 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
235 filehandle = _lcreat( filename, 2 );
236 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
238 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
240 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
242 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
244 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
246 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
248 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
250 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
251 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError( ) );
253 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
255 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
257 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
259 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
261 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
263 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
265 filehandle=_lcreat (slashname, 0); /* illegal name */
266 if (HFILE_ERROR==filehandle) {
267 err=GetLastError ();
268 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
269 "creating file \"%s\" failed with error %d\n", slashname, err);
270 } else { /* only NT succeeds */
271 _lclose(filehandle);
272 find=FindFirstFileA (slashname, &search_results);
273 if (INVALID_HANDLE_VALUE!=find)
275 ok (0!=FindClose (find), "FindClose complains (%ld)\n", GetLastError ());
276 slashname[strlen(slashname)-1]=0;
277 ok (!strcmp (slashname, search_results.cFileName),
278 "found unexpected name \"%s\"\n", search_results.cFileName);
279 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
280 "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
281 search_results.dwFileAttributes);
283 ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)\n", slashname,
284 GetLastError ());
287 filehandle=_lcreat (filename, 8); /* illegal attribute */
288 if (HFILE_ERROR==filehandle)
289 ok (0, "couldn't create volume label \"%s\"\n", 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\n", filename);
295 else {
296 ok (0!=FindClose (find), "FindClose complains (%ld)\n", GetLastError ());
297 ok (!strcmp (filename, search_results.cFileName),
298 "found unexpected name \"%s\"\n", search_results.cFileName);
299 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
300 "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
301 search_results.dwFileAttributes);
303 ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)\n", slashname,
304 GetLastError ());
309 static 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)\n",filename,GetLastError());
320 return;
323 for (i = 0; i < 400; i++)
325 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
327 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
328 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
330 bytes_read = _hread( filehandle, buffer, 1);
331 ok( 1 == bytes_read, "file read size error\n" );
332 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
333 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
335 bytes_read = _hread( filehandle, buffer, 1);
336 ok( 1 == bytes_read, "file read size error\n" );
337 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
338 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
339 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
341 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", 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)\n",filename,GetLastError());
355 return;
358 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
359 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
361 filehandle = _lopen( filename, OF_READ );
362 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
363 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
364 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
365 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
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\n" );
370 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
371 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
373 filehandle = _lopen( filename, OF_WRITE );
374 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
375 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
376 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
378 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", 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)\n",filename,GetLastError());
395 return;
398 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
400 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
402 filehandle = _lopen( filename, OF_READ );
404 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError());
406 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
408 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
410 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
412 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
413 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
414 for (i = 0; i < bytes_wanted; i++)
416 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
420 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
422 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", 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)\n",filename,GetLastError());
442 return;
445 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
447 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
449 filehandle = _lopen( filename, OF_READ );
451 bytes_read = _hread( filehandle, buffer, 1);
453 ok( 0 == bytes_read, "file read size error\n" );
455 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
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\n" );
470 bytes_written = bytes_written + sizeof( buffer );
473 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
474 bytes_written++;
476 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
478 memory_object = LocalAlloc( LPTR, bytes_written );
480 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
482 contents = LocalLock( memory_object );
484 filehandle = _lopen( filename, OF_READ );
486 contents = LocalLock( memory_object );
488 ok( NULL != contents, "LocalLock whines\n" );
490 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
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\n" );
503 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
505 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)\n", GetLastError( ) );
508 static 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\n", GetLastError());
517 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
519 ret = GetTempFileNameA(temp_path, prefix, 0, source);
520 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
522 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
523 ok(ret != 0, "GetTempFileNameA error %ld\n", 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 static 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\n", GetLastError());
549 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
551 ret = GetTempFileNameW(temp_path, prefix, 0, source);
552 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
554 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
555 ok(ret != 0, "GetTempFileNameW error %ld\n", 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 static 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\n", GetLastError());
580 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
582 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
583 ok(ret != 0, "GetTempFileNameA error %ld\n", 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\n");
590 ret = DeleteFileA(filename);
591 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
594 static 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\n", GetLastError());
606 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
608 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
609 ok(ret != 0, "GetTempFileNameW error %ld\n", 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\n");
616 ret = DeleteFileW(filename);
617 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
621 static void test_GetTempFileNameA() {
622 UINT result;
623 char out[MAX_PATH];
624 char *expected = "c:\\windows\\abc2.tmp";
626 /* this test may depend on the config file settings */
627 result = GetTempFileNameA("C:", "abc", 1, out);
628 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
629 ok( ((out[0] == 'C') && (out[1] == ':')) && (out[2] == '\\'), "GetTempFileNameA: first three characters should be C:\\, string was actually %s\n", out );
631 result = GetTempFileNameA("c:\\windows\\", "abc", 2, out);
632 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
633 ok( lstrcmpiA( out, expected ) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n", out, expected );
637 static void test_DeleteFileA( void )
639 BOOL ret;
641 ret = DeleteFileA(NULL);
642 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
643 GetLastError() == ERROR_PATH_NOT_FOUND),
644 "DeleteFileA(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
646 ret = DeleteFileA("");
647 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
648 GetLastError() == ERROR_BAD_PATHNAME),
649 "DeleteFileA(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
652 static void test_DeleteFileW( void )
654 BOOL ret;
655 WCHAR emptyW[]={'\0'};
657 ret = DeleteFileW(NULL);
658 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
659 return;
660 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
661 "DeleteFileW(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
663 ret = DeleteFileW(emptyW);
664 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
665 "DeleteFileW(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
668 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
670 static void test_MoveFileA(void)
672 char tempdir[MAX_PATH];
673 char source[MAX_PATH], dest[MAX_PATH];
674 static const char prefix[] = "pfx";
675 DWORD ret;
677 ret = GetTempPathA(MAX_PATH, tempdir);
678 ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
679 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
681 ret = GetTempFileNameA(tempdir, prefix, 0, source);
682 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
684 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
685 ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
687 ret = MoveFileA(source, dest);
688 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
689 "MoveFileA: unexpected error %ld\n", GetLastError());
691 ret = DeleteFileA(dest);
692 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
694 ret = MoveFileA(source, dest);
695 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
697 lstrcatA(tempdir, "Remove Me");
698 ret = CreateDirectoryA(tempdir, NULL);
699 ok(ret == TRUE, "CreateDirectoryA failed\n");
701 lstrcpyA(source, dest);
702 lstrcpyA(dest, tempdir);
703 lstrcatA(dest, "\\wild?.*");
704 ret = MoveFileA(source, dest);
705 todo_wine {
706 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
707 ok(GetLastError() == ERROR_INVALID_NAME,
708 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
709 #if 0
710 if (ret || (GetLastError() != ERROR_INVALID_NAME))
712 WIN32_FIND_DATAA fd;
713 char temppath[MAX_PATH];
714 HANDLE hFind;
716 lstrcpyA(temppath, tempdir);
717 lstrcatA(temppath, "\\*.*");
718 hFind = FindFirstFileA(temppath, &fd);
719 if (INVALID_HANDLE_VALUE != hFind)
721 LPSTR lpName;
724 lpName = fd.cAlternateFileName;
725 if (!lpName[0])
726 lpName = fd.cFileName;
727 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
729 while (FindNextFileA(hFind, &fd));
730 FindClose(hFind);
733 #endif
734 ret = DeleteFileA(source);
735 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
736 ret = DeleteFileA(dest);
737 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
740 ret = RemoveDirectoryA(tempdir);
741 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
744 static void test_MoveFileW(void)
746 WCHAR temp_path[MAX_PATH];
747 WCHAR source[MAX_PATH], dest[MAX_PATH];
748 static const WCHAR prefix[] = {'p','f','x',0};
749 DWORD ret;
751 ret = GetTempPathW(MAX_PATH, temp_path);
752 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
753 return;
754 ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
755 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
757 ret = GetTempFileNameW(temp_path, prefix, 0, source);
758 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
760 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
761 ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
763 ret = MoveFileW(source, dest);
764 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
765 "CopyFileW: unexpected error %ld\n", GetLastError());
767 ret = DeleteFileW(source);
768 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
769 ret = DeleteFileW(dest);
770 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
773 #define PATTERN_OFFSET 0x10
775 static void test_offset_in_overlapped_structure(void)
777 HANDLE hFile;
778 OVERLAPPED ov;
779 DWORD done;
780 BOOL rc;
781 BYTE buf[256], pattern[] = "TeSt";
782 UINT i;
783 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
785 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld\n", GetLastError());
786 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld\n", GetLastError());
788 /*** Write File *****************************************************/
790 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
791 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
793 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
794 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld\n", GetLastError());
795 ok(done == sizeof(buf), "expected number of bytes written %lu\n", done);
797 memset(&ov, 0, sizeof(ov));
798 ov.Offset = PATTERN_OFFSET;
799 ov.OffsetHigh = 0;
800 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
801 /* Win 9x does not support the overlapped I/O on files */
802 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
803 ok(rc, "WriteFile error %ld\n", GetLastError());
804 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
805 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
806 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
807 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
809 ov.Offset = sizeof(buf) * 2;
810 ov.OffsetHigh = 0;
811 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld\n", GetLastError());
812 ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
813 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
814 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
815 "expected file offset %d\n", sizeof(buf) * 2 + sizeof(pattern));
818 CloseHandle(hFile);
820 /*** Read File *****************************************************/
822 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
823 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
825 memset(buf, 0, sizeof(buf));
826 memset(&ov, 0, sizeof(ov));
827 ov.Offset = PATTERN_OFFSET;
828 ov.OffsetHigh = 0;
829 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
830 /* Win 9x does not support the overlapped I/O on files */
831 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
832 ok(rc, "ReadFile error %ld\n", GetLastError());
833 ok(done == sizeof(pattern), "expected number of bytes read %lu\n", done);
834 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
835 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
836 "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
837 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
840 CloseHandle(hFile);
842 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
845 static void test_LockFile(void)
847 HANDLE handle;
848 DWORD written;
849 OVERLAPPED overlapped;
851 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
852 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
853 CREATE_ALWAYS, 0, 0 );
854 if (handle == INVALID_HANDLE_VALUE)
856 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
857 return;
859 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
861 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
862 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
863 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded\n" );
865 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
866 /* overlapping locks must fail */
867 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
868 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
869 /* non-overlapping locks must succeed */
870 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
872 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
873 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
874 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
875 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
877 overlapped.Offset = 100;
878 overlapped.OffsetHigh = 0;
879 overlapped.hEvent = 0;
880 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed\n" );
881 /* overlapping shared locks are OK */
882 overlapped.Offset = 150;
883 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
884 /* but exclusive is not */
885 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
886 "LockFileEx exclusive 150,50 succeeded\n" );
887 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed\n" );
888 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded\n" );
889 overlapped.Offset = 100;
890 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed\n" );
891 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded\n" );
893 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
894 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
895 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
896 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
898 /* wrap-around lock should not do anything */
899 /* (but still succeeds on NT4 so we don't check result) */
900 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
901 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed\n" );
902 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
904 /* zero-byte lock */
905 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
906 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
907 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
908 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
909 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
910 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
911 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
913 CloseHandle( handle );
914 DeleteFileA( filename );
917 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
919 if (!access1 || !access2) return 1;
920 if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
921 if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
922 if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
923 if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
924 return 1;
927 static void test_file_sharing(void)
929 static const DWORD access_modes[4] = { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE };
930 static const DWORD sharing_modes[4] = { 0, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE };
931 int a1, s1, a2, s2;
933 for (a1 = 0; a1 < 4; a1++)
935 for (s1 = 0; s1 < 4; s1++)
937 HANDLE h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
938 NULL, CREATE_ALWAYS, 0, 0 );
939 if (h == INVALID_HANDLE_VALUE)
941 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
942 return;
944 for (a2 = 0; a2 < 4; a2++)
946 for (s2 = 0; s2 < 4; s2++)
948 HANDLE h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
949 NULL, CREATE_ALWAYS, 0, 0 );
950 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
951 access_modes[a2], sharing_modes[s2] ))
953 ok( h2 != INVALID_HANDLE_VALUE,
954 "open failed for modes %lx/%lx/%lx/%lx err %ld\n",
955 access_modes[a1], sharing_modes[s1],
956 access_modes[a2], sharing_modes[s2], GetLastError() );
958 else
960 ok( h2 == INVALID_HANDLE_VALUE,
961 "open succeeded for modes %lx/%lx/%lx/%lx\n",
962 access_modes[a1], sharing_modes[s1],
963 access_modes[a2], sharing_modes[s2] );
964 if (h2 == INVALID_HANDLE_VALUE)
965 ok( GetLastError() == ERROR_SHARING_VIOLATION,
966 "wrong error code %ld\n", GetLastError() );
968 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
971 CloseHandle( h );
974 DeleteFileA( filename );
977 static void test_FindFirstFileA()
979 HANDLE handle;
980 WIN32_FIND_DATAA search_results;
981 int err;
983 handle = FindFirstFileA("C:\\",&search_results);
984 err = GetLastError();
985 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail\n");
986 if (handle == INVALID_HANDLE_VALUE)
987 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
988 handle = FindFirstFileA("C:\\*",&search_results);
989 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
990 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
993 static void test_FindNextFileA()
995 HANDLE handle;
996 WIN32_FIND_DATAA search_results;
997 int err;
999 handle = FindFirstFileA("C:\\*",&search_results);
1000 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1001 while (FindNextFile(handle, &search_results))
1003 /* get to the end of the files */
1005 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1006 err = GetLastError();
1007 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1010 static void test_MapFile()
1012 HANDLE handle, hmap;
1014 /* be sure to remove stale files */
1015 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1016 DeleteFile(filename);
1017 handle = CreateFile( filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1018 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
1019 ok( handle != INVALID_HANDLE_VALUE, "couldn't create test file\n");
1021 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1022 ok( hmap == NULL, "mapped zero size file\n");
1023 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1025 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
1026 ok( hmap == NULL, "mapping should fail\n");
1027 /* GetLastError() varies between win9x and WinNT */
1029 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
1030 ok( hmap == NULL, "mapping should fail\n");
1031 /* GetLastError() varies between win9x and WinNT */
1033 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, NULL );
1034 ok( hmap != NULL, "mapping should succeed\n");
1036 ok( CloseHandle( hmap ), "can't close mapping handle\n");
1037 ok( CloseHandle( handle ), "can't close file handle\n");
1038 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1041 START_TEST(file)
1043 test__hread( );
1044 test__hwrite( );
1045 test__lclose( );
1046 test__lcreat( );
1047 test__llseek( );
1048 test__llopen( );
1049 test__lread( );
1050 test__lwrite( );
1051 test_GetTempFileNameA();
1052 test_CopyFileA();
1053 test_CopyFileW();
1054 test_CreateFileA();
1055 test_CreateFileW();
1056 test_DeleteFileA();
1057 test_DeleteFileW();
1058 test_MoveFileA();
1059 test_MoveFileW();
1060 test_FindFirstFileA();
1061 test_FindNextFileA();
1062 test_LockFile();
1063 test_file_sharing();
1064 test_offset_in_overlapped_structure();
1065 test_MapFile();