Implemented file locking functions (partly based on my old Corel
[wine] / dlls / kernel / tests / file.c
1 /*
2  * Unit tests for file functions in Wine
3  *
4  * Copyright (c) 2002 Jakob Eriksson
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdlib.h>
23 #include <time.h>
24
25 #include "wine/test.h"
26 #include "winbase.h"
27 #include "winerror.h"
28
29
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 ";
42
43
44 static void test__hread( void )
45 {
46     HFILE filehandle;
47     char buffer[10000];
48     long bytes_read;
49     long bytes_wanted;
50     long i;
51
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)
56     {
57         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
58         return;
59     }
60
61     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
62
63     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
64
65     filehandle = _lopen( filename, OF_READ );
66
67     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError(  ) );
68
69     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
70
71     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
72
73     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
74     {
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++)
78         {
79             ok( buffer[i] == sillytext[i], "that's not what's written" );
80         }
81     }
82
83     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
84
85     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
86 }
87
88
89 static void test__hwrite( void )
90 {
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];
100
101     filehandle = _lcreat( filename, 0 );
102     if (filehandle == HFILE_ERROR)
103     {
104         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
105         return;
106     }
107
108     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
109
110     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
111
112     filehandle = _lopen( filename, OF_READ );
113
114     bytes_read = _hread( filehandle, buffer, 1);
115
116     ok( 0 == bytes_read, "file read size error" );
117
118     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
119
120     filehandle = _lopen( filename, OF_READWRITE );
121
122     bytes_written = 0;
123     checksum[0] = '\0';
124     srand( (unsigned)time( NULL ) );
125     for (blocks = 0; blocks < 100; blocks++)
126     {
127         for (i = 0; i < sizeof( buffer ); i++)
128         {
129             buffer[i] = rand(  );
130             checksum[0] = checksum[0] + buffer[i];
131         }
132         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
133         bytes_written = bytes_written + sizeof( buffer );
134     }
135
136     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
137     bytes_written++;
138
139     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
140
141     memory_object = LocalAlloc( LPTR, bytes_written );
142
143     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
144
145     contents = LocalLock( memory_object );
146
147     filehandle = _lopen( filename, OF_READ );
148
149     contents = LocalLock( memory_object );
150
151     ok( NULL != contents, "LocalLock whines" );
152
153     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
154
155     checksum[0] = '\0';
156     i = 0;
157     do
158     {
159         checksum[0] = checksum[0] + contents[i];
160         i++;
161     }
162     while (i < bytes_written - 1);
163
164     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
165
166     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
167
168     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
169 }
170
171
172 static void test__lclose( void )
173 {
174     HFILE filehandle;
175
176     filehandle = _lcreat( filename, 0 );
177     if (filehandle == HFILE_ERROR)
178     {
179         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
180         return;
181     }
182
183     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
184
185     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
186
187     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
188 }
189
190
191 static void test__lcreat( void )
192 {
193     HFILE filehandle;
194     char buffer[10000];
195     WIN32_FIND_DATAA search_results;
196
197     filehandle = _lcreat( filename, 0 );
198     if (filehandle == HFILE_ERROR)
199     {
200         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
201         return;
202     }
203
204     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
205
206     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
207
208     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
209
210     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
211
212     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
213
214     ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
215
216     filehandle = _lcreat( filename, 1 ); /* readonly */
217     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
218
219     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
220
221     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
222
223     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
224
225     ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
226
227     ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
228
229     ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
230
231     filehandle = _lcreat( filename, 2 );
232     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
233
234     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
235
236     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
237
238     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
239
240     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
241
242     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
243
244     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
245
246     filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
247     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
248
249     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
250
251     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
252
253     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
254
255     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
256
257     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
258
259     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
260 }
261
262
263 void test__llseek( void )
264 {
265     INT i;
266     HFILE filehandle;
267     char buffer[1];
268     long bytes_read;
269
270     filehandle = _lcreat( filename, 0 );
271     if (filehandle == HFILE_ERROR)
272     {
273         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
274         return;
275     }
276
277     for (i = 0; i < 400; i++)
278     {
279         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
280     }
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" );
283
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" );
288
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" );
294
295     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
296 }
297
298
299 static void test__llopen( void )
300 {
301     HFILE filehandle;
302     UINT bytes_read;
303     char buffer[10000];
304
305     filehandle = _lcreat( filename, 0 );
306     if (filehandle == HFILE_ERROR)
307     {
308         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
309         return;
310     }
311
312     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
313     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
314
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" );
320
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" );
326
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" );
331
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 */
334 }
335
336
337 static void test__lread( void )
338 {
339     HFILE filehandle;
340     char buffer[10000];
341     long bytes_read;
342     UINT bytes_wanted;
343     UINT i;
344
345     filehandle = _lcreat( filename, 0 );
346     if (filehandle == HFILE_ERROR)
347     {
348         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
349         return;
350     }
351
352     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
353
354     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
355
356     filehandle = _lopen( filename, OF_READ );
357
358     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
359
360     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
361
362     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
363
364     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
365     {
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++)
369         {
370             ok( buffer[i] == sillytext[i], "that's not what's written" );
371         }
372     }
373
374     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
375
376     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
377 }
378
379
380 static void test__lwrite( void )
381 {
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];
391
392     filehandle = _lcreat( filename, 0 );
393     if (filehandle == HFILE_ERROR)
394     {
395         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
396         return;
397     }
398
399     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
400
401     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
402
403     filehandle = _lopen( filename, OF_READ );
404
405     bytes_read = _hread( filehandle, buffer, 1);
406
407     ok( 0 == bytes_read, "file read size error" );
408
409     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
410
411     filehandle = _lopen( filename, OF_READWRITE );
412
413     bytes_written = 0;
414     checksum[0] = '\0';
415     srand( (unsigned)time( NULL ) );
416     for (blocks = 0; blocks < 100; blocks++)
417     {
418         for (i = 0; i < sizeof( buffer ); i++)
419         {
420             buffer[i] = rand(  );
421             checksum[0] = checksum[0] + buffer[i];
422         }
423         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
424         bytes_written = bytes_written + sizeof( buffer );
425     }
426
427     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
428     bytes_written++;
429
430     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
431
432     memory_object = LocalAlloc( LPTR, bytes_written );
433
434     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
435
436     contents = LocalLock( memory_object );
437
438     filehandle = _lopen( filename, OF_READ );
439
440     contents = LocalLock( memory_object );
441
442     ok( NULL != contents, "LocalLock whines" );
443
444     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
445
446     checksum[0] = '\0';
447     i = 0;
448     do
449     {
450         checksum[0] += contents[i];
451         i++;
452     }
453     while (i < bytes_written - 1);
454
455     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
456
457     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
458
459     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
460 }
461
462 void test_CopyFileA(void)
463 {
464     char temp_path[MAX_PATH];
465     char source[MAX_PATH], dest[MAX_PATH];
466     static const char prefix[] = "pfx";
467     DWORD ret;
468
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");
472
473     ret = GetTempFileNameA(temp_path, prefix, 0, source);
474     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
475
476     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
477     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
478
479     ret = CopyFileA(source, dest, TRUE);
480     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
481        "CopyFileA: unexpected error %ld\n", GetLastError());
482
483     ret = CopyFileA(source, dest, FALSE);
484     ok(ret,  "CopyFileA: error %ld\n", GetLastError());
485
486     ret = DeleteFileA(source);
487     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
488     ret = DeleteFileA(dest);
489     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
490 }
491
492 void test_CopyFileW(void)
493 {
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;
498
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");
504
505     ret = GetTempFileNameW(temp_path, prefix, 0, source);
506     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
507
508     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
509     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
510
511     ret = CopyFileW(source, dest, TRUE);
512     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
513        "CopyFileW: unexpected error %ld\n", GetLastError());
514
515     ret = CopyFileW(source, dest, FALSE);
516     ok(ret,  "CopyFileW: error %ld\n", GetLastError());
517
518     ret = DeleteFileW(source);
519     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
520     ret = DeleteFileW(dest);
521     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
522 }
523
524 void test_CreateFileA(void)
525 {
526     HANDLE hFile;
527     char temp_path[MAX_PATH];
528     char filename[MAX_PATH];
529     static const char prefix[] = "pfx";
530     DWORD ret;
531
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");
535
536     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
537     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
538
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");
543
544     ret = DeleteFileA(filename);
545     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
546 }
547
548 void test_CreateFileW(void)
549 {
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;
555
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");
561
562     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
563     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
564
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");
569
570     ret = DeleteFileW(filename);
571     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
572 }
573
574 static void test_DeleteFileA( void )
575 {
576     BOOL ret;
577
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());
582
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());
587 }
588
589 static void test_DeleteFileW( void )
590 {
591     BOOL ret;
592     WCHAR emptyW[]={'\0'};
593
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());
599
600     ret = DeleteFileW(emptyW);
601     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
602        "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
603 }
604
605 #define PATTERN_OFFSET 0x10
606
607 void test_offset_in_overlapped_structure(void)
608 {
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];
616
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());
619
620     /*** Write File *****************************************************/
621
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());
624
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);
628
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));
640
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));
648     }
649
650     CloseHandle(hFile);
651
652     /*** Read File *****************************************************/
653
654     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
655     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
656
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");
670     }
671
672     CloseHandle(hFile);
673
674     ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
675 }
676
677 static void test_LockFile(void)
678 {
679     HANDLE handle;
680     DWORD written;
681     OVERLAPPED overlapped;
682
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)
687     {
688         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
689         return;
690     }
691     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
692
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" );
696
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" );
703
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" );
708
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" );
724
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" );
729
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" );
735
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" );
744
745     CloseHandle( handle );
746     DeleteFileA( filename );
747 }
748
749
750 START_TEST(file)
751 {
752     test__hread(  );
753     test__hwrite(  );
754     test__lclose(  );
755     test__lcreat(  );
756     test__llseek(  );
757     test__llopen(  );
758     test__lread(  );
759     test__lwrite(  );
760     test_CopyFileA();
761     test_CopyFileW();
762     test_CreateFileA();
763     test_CreateFileW();
764     test_DeleteFileA();
765     test_DeleteFileW();
766     test_LockFile();
767     test_offset_in_overlapped_structure();
768 }