kernel32: Print more information about the reason of a test failure.
[wine] / dlls / kernel32 / tests / file.c
1 /*
2  * Unit tests for file functions in Wine
3  *
4  * Copyright (c) 2002, 2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30
31 static int dll_capable(const char *dll, const char *function)
32 {
33     HMODULE module = GetModuleHandleA(dll);
34     if (!module) return 0;
35
36     return (GetProcAddress(module, function) != NULL);
37 }
38
39 /* keep filename and filenameW the same */
40 static const char filename[] = "testfile.xxx";
41 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
42 static const char sillytext[] =
43 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
47 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
52 "sdlkfjasdlkfj a dslkj adsklf  \n  \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
53
54
55 static void test__hread( void )
56 {
57     HFILE filehandle;
58     char buffer[10000];
59     long bytes_read;
60     long bytes_wanted;
61     long i;
62     BOOL ret;
63
64     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
65     DeleteFileA( filename );
66     filehandle = _lcreat( filename, 0 );
67     if (filehandle == HFILE_ERROR)
68     {
69         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
70         return;
71     }
72
73     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
74
75     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
76
77     filehandle = _lopen( filename, OF_READ );
78
79     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError(  ) );
80
81     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
82
83     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
84
85     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
86     {
87         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
88         ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
89         for (i = 0; i < bytes_wanted; i++)
90         {
91             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
92         }
93     }
94
95     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
96
97     ret = DeleteFileA( filename );
98     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError(  ) );
99 }
100
101
102 static void test__hwrite( void )
103 {
104     HFILE filehandle;
105     char buffer[10000];
106     long bytes_read;
107     long bytes_written;
108     long blocks;
109     long i;
110     char *contents;
111     HLOCAL memory_object;
112     char checksum[1];
113     BOOL ret;
114
115     filehandle = _lcreat( filename, 0 );
116     if (filehandle == HFILE_ERROR)
117     {
118         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
119         return;
120     }
121
122     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
123
124     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
125
126     filehandle = _lopen( filename, OF_READ );
127
128     bytes_read = _hread( filehandle, buffer, 1);
129
130     ok( 0 == bytes_read, "file read size error\n" );
131
132     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
133
134     filehandle = _lopen( filename, OF_READWRITE );
135
136     bytes_written = 0;
137     checksum[0] = '\0';
138     srand( (unsigned)time( NULL ) );
139     for (blocks = 0; blocks < 100; blocks++)
140     {
141         for (i = 0; i < (long)sizeof( buffer ); i++)
142         {
143             buffer[i] = rand(  );
144             checksum[0] = checksum[0] + buffer[i];
145         }
146         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
147         bytes_written = bytes_written + sizeof( buffer );
148     }
149
150     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
151     bytes_written++;
152
153     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
154
155     memory_object = LocalAlloc( LPTR, bytes_written );
156
157     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
158
159     contents = LocalLock( memory_object );
160
161     filehandle = _lopen( filename, OF_READ );
162
163     contents = LocalLock( memory_object );
164
165     ok( NULL != contents, "LocalLock whines\n" );
166
167     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
168
169     checksum[0] = '\0';
170     i = 0;
171     do
172     {
173         checksum[0] = checksum[0] + contents[i];
174         i++;
175     }
176     while (i < bytes_written - 1);
177
178     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
179
180     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
181
182     ret = DeleteFileA( filename );
183     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError(  ) );
184 }
185
186
187 static void test__lclose( void )
188 {
189     HFILE filehandle;
190     BOOL ret;
191
192     filehandle = _lcreat( filename, 0 );
193     if (filehandle == HFILE_ERROR)
194     {
195         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
196         return;
197     }
198
199     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
200
201     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
202
203     ret = DeleteFileA( filename );
204     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError(  ) );
205 }
206
207
208 static void test__lcreat( void )
209 {
210     HFILE filehandle;
211     char buffer[10000];
212     WIN32_FIND_DATAA search_results;
213     char slashname[] = "testfi/";
214     int err;
215     HANDLE find;
216     BOOL ret;
217
218     filehandle = _lcreat( filename, 0 );
219     if (filehandle == HFILE_ERROR)
220     {
221         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
222         return;
223     }
224
225     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
226
227     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
228
229     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
230
231     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
232
233     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
234
235     ret = DeleteFileA(filename);
236     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
237
238     filehandle = _lcreat( filename, 1 ); /* readonly */
239     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
240
241     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
242
243     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
244
245     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
246
247     ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
248
249     ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
250
251     ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
252
253     filehandle = _lcreat( filename, 2 );
254     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
255
256     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
257
258     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
259
260     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
261
262     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
263
264     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
265
266     ret = DeleteFileA( filename );
267     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
268
269     filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
270     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
271
272     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
273
274     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
275
276     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
277
278     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
279
280     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
281
282     ret = DeleteFileA( filename );
283     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
284
285     filehandle=_lcreat (slashname, 0); /* illegal name */
286     if (HFILE_ERROR==filehandle) {
287       err=GetLastError ();
288       ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
289           "creating file \"%s\" failed with error %d\n", slashname, err);
290     } else { /* only NT succeeds */
291       _lclose(filehandle);
292       find=FindFirstFileA (slashname, &search_results);
293       if (INVALID_HANDLE_VALUE!=find)
294       {
295         ret = FindClose (find);
296         ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
297         slashname[strlen(slashname)-1]=0;
298         ok (!strcmp (slashname, search_results.cFileName),
299             "found unexpected name \"%s\"\n", search_results.cFileName);
300         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301             "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
302             search_results.dwFileAttributes);
303       }
304     ret = DeleteFileA( slashname );
305     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
306     }
307
308     filehandle=_lcreat (filename, 8); /* illegal attribute */
309     if (HFILE_ERROR==filehandle)
310       ok (0, "couldn't create volume label \"%s\"\n", filename);
311     else {
312       _lclose(filehandle);
313       find=FindFirstFileA (filename, &search_results);
314       if (INVALID_HANDLE_VALUE==find)
315         ok (0, "file \"%s\" not found\n", filename);
316       else {
317         ret = FindClose(find);
318         ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
319         ok (!strcmp (filename, search_results.cFileName),
320             "found unexpected name \"%s\"\n", search_results.cFileName);
321         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
322             "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
323             search_results.dwFileAttributes);
324       }
325     ret = DeleteFileA( filename );
326     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
327     }
328 }
329
330
331 static void test__llseek( void )
332 {
333     INT i;
334     HFILE filehandle;
335     char buffer[1];
336     long bytes_read;
337     BOOL ret;
338
339     filehandle = _lcreat( filename, 0 );
340     if (filehandle == HFILE_ERROR)
341     {
342         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
343         return;
344     }
345
346     for (i = 0; i < 400; i++)
347     {
348         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
349     }
350     ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
351     ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
352
353     bytes_read = _hread( filehandle, buffer, 1);
354     ok( 1 == bytes_read, "file read size error\n" );
355     ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
356     ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
357
358     bytes_read = _hread( filehandle, buffer, 1);
359     ok( 1 == bytes_read, "file read size error\n" );
360     ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
361     ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
362     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
363
364     ret = DeleteFileA( filename );
365     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
366 }
367
368
369 static void test__llopen( void )
370 {
371     HFILE filehandle;
372     UINT bytes_read;
373     char buffer[10000];
374     BOOL ret;
375
376     filehandle = _lcreat( filename, 0 );
377     if (filehandle == HFILE_ERROR)
378     {
379         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
380         return;
381     }
382
383     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
384     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
385
386     filehandle = _lopen( filename, OF_READ );
387     ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
388     bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
389     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
390     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
391
392     filehandle = _lopen( filename, OF_READWRITE );
393     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
394     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
395     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
396     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
397
398     filehandle = _lopen( filename, OF_WRITE );
399     ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
400     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
401     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
402
403     ret = DeleteFileA( filename );
404     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
405     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
406 }
407
408
409 static void test__lread( void )
410 {
411     HFILE filehandle;
412     char buffer[10000];
413     long bytes_read;
414     UINT bytes_wanted;
415     UINT i;
416     BOOL ret;
417
418     filehandle = _lcreat( filename, 0 );
419     if (filehandle == HFILE_ERROR)
420     {
421         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
422         return;
423     }
424
425     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
426
427     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
428
429     filehandle = _lopen( filename, OF_READ );
430
431     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
432
433     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
434
435     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
436
437     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
438     {
439         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
440         ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
441         for (i = 0; i < bytes_wanted; i++)
442         {
443             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
444         }
445     }
446
447     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
448
449     ret = DeleteFileA( filename );
450     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
451 }
452
453
454 static void test__lwrite( void )
455 {
456     HFILE filehandle;
457     char buffer[10000];
458     long bytes_read;
459     long bytes_written;
460     long blocks;
461     long i;
462     char *contents;
463     HLOCAL memory_object;
464     char checksum[1];
465     BOOL ret;
466
467     filehandle = _lcreat( filename, 0 );
468     if (filehandle == HFILE_ERROR)
469     {
470         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
471         return;
472     }
473
474     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
475
476     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
477
478     filehandle = _lopen( filename, OF_READ );
479
480     bytes_read = _hread( filehandle, buffer, 1);
481
482     ok( 0 == bytes_read, "file read size error\n" );
483
484     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
485
486     filehandle = _lopen( filename, OF_READWRITE );
487
488     bytes_written = 0;
489     checksum[0] = '\0';
490     srand( (unsigned)time( NULL ) );
491     for (blocks = 0; blocks < 100; blocks++)
492     {
493         for (i = 0; i < (long)sizeof( buffer ); i++)
494         {
495             buffer[i] = rand(  );
496             checksum[0] = checksum[0] + buffer[i];
497         }
498         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
499         bytes_written = bytes_written + sizeof( buffer );
500     }
501
502     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
503     bytes_written++;
504
505     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
506
507     memory_object = LocalAlloc( LPTR, bytes_written );
508
509     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
510
511     contents = LocalLock( memory_object );
512
513     filehandle = _lopen( filename, OF_READ );
514
515     contents = LocalLock( memory_object );
516
517     ok( NULL != contents, "LocalLock whines\n" );
518
519     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
520
521     checksum[0] = '\0';
522     i = 0;
523     do
524     {
525         checksum[0] += contents[i];
526         i++;
527     }
528     while (i < bytes_written - 1);
529
530     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
531
532     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
533
534     ret = DeleteFileA( filename );
535     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
536 }
537
538 static void test_CopyFileA(void)
539 {
540     char temp_path[MAX_PATH];
541     char source[MAX_PATH], dest[MAX_PATH];
542     static const char prefix[] = "pfx";
543     HANDLE hfile;
544     FILETIME ft1, ft2;
545     char buf[10];
546     DWORD ret;
547     BOOL retok;
548
549     ret = GetTempPathA(MAX_PATH, temp_path);
550     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
551     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
552
553     ret = GetTempFileNameA(temp_path, prefix, 0, source);
554     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
555
556     /* make the source have not zero size */
557     hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
558     ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
559     retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
560     ok( retok && ret == sizeof(prefix),
561        "WriteFile error %d\n", GetLastError());
562     ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
563     /* get the file time and change it to prove the difference */
564     ret = GetFileTime(hfile, NULL, NULL, &ft1);
565     ok( ret, "GetFileTime error %d\n", GetLastError());
566     ft1.dwLowDateTime -= 600000000; /* 60 second */
567     ret = SetFileTime(hfile, NULL, NULL, &ft1);
568     ok( ret, "SetFileTime error %d\n", GetLastError());
569     GetFileTime(hfile, NULL, NULL, &ft1);  /* get the actual time back */
570     CloseHandle(hfile);
571
572     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
573     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
574
575     SetLastError(0xdeadbeef);
576     ret = CopyFileA(source, dest, TRUE);
577     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
578        "CopyFileA: unexpected error %d\n", GetLastError());
579
580     ret = CopyFileA(source, dest, FALSE);
581     ok(ret, "CopyFileA: error %d\n", GetLastError());
582
583     /* make sure that destination has correct size */
584     hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
585     ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
586     ret = GetFileSize(hfile, NULL);
587     ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
588
589     /* make sure that destination has the same filetime */
590     ret = GetFileTime(hfile, NULL, NULL, &ft2);
591     ok( ret, "GetFileTime error %d\n", GetLastError());
592     ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
593
594     SetLastError(0xdeadbeef);
595     ret = CopyFileA(source, dest, FALSE);
596     ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
597        "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
598
599     /* make sure that destination still has correct size */
600     ret = GetFileSize(hfile, NULL);
601     ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
602     retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
603     ok( retok && ret == sizeof(prefix),
604        "ReadFile: error %d\n", GetLastError());
605     ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
606     CloseHandle(hfile);
607
608     ret = DeleteFileA(source);
609     ok(ret, "DeleteFileA: error %d\n", GetLastError());
610     ret = DeleteFileA(dest);
611     ok(ret, "DeleteFileA: error %d\n", GetLastError());
612 }
613
614 static void test_CopyFileW(void)
615 {
616     WCHAR temp_path[MAX_PATH];
617     WCHAR source[MAX_PATH], dest[MAX_PATH];
618     static const WCHAR prefix[] = {'p','f','x',0};
619     DWORD ret;
620
621     ret = GetTempPathW(MAX_PATH, temp_path);
622     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
623         return;
624     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
625     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
626
627     ret = GetTempFileNameW(temp_path, prefix, 0, source);
628     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
629
630     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
631     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
632
633     ret = CopyFileW(source, dest, TRUE);
634     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
635        "CopyFileW: unexpected error %d\n", GetLastError());
636
637     ret = CopyFileW(source, dest, FALSE);
638     ok(ret, "CopyFileW: error %d\n", GetLastError());
639
640     ret = DeleteFileW(source);
641     ok(ret, "DeleteFileW: error %d\n", GetLastError());
642     ret = DeleteFileW(dest);
643     ok(ret, "DeleteFileW: error %d\n", GetLastError());
644 }
645
646 static void test_CreateFileA(void)
647 {
648     HANDLE hFile;
649     char temp_path[MAX_PATH];
650     char filename[MAX_PATH];
651     static const char prefix[] = "pfx";
652     DWORD ret;
653
654     ret = GetTempPathA(MAX_PATH, temp_path);
655     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
656     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
657
658     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
659     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
660
661     hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
662                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
663     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
664         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
665
666     ret = DeleteFileA(filename);
667     ok(ret, "DeleteFileA: error %d\n", GetLastError());
668 }
669
670 static void test_CreateFileW(void)
671 {
672     HANDLE hFile;
673     WCHAR temp_path[MAX_PATH];
674     WCHAR filename[MAX_PATH];
675     static const WCHAR emptyW[]={'\0'};
676     static const WCHAR prefix[] = {'p','f','x',0};
677     static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
678     DWORD ret;
679
680     ret = GetTempPathW(MAX_PATH, temp_path);
681     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
682         return;
683     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
684     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
685
686     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
687     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
688
689     hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
690                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
691     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
692         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
693
694     ret = DeleteFileW(filename);
695     ok(ret, "DeleteFileW: error %d\n", GetLastError());
696
697     if (0)
698     {
699         /* this crashes on NT4.0 */
700         hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
701                             CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
702         ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
703            "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
704     }
705
706     hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
707                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
708     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
709        "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
710
711     /* test the result of opening a nonexistent driver name */
712     hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
713                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
714     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
715        "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
716 }
717
718 static void test_GetTempFileNameA(void)
719 {
720     UINT result;
721     char out[MAX_PATH];
722     char expected[MAX_PATH + 10];
723     char windowsdir[MAX_PATH + 10];
724     char windowsdrive[3];
725
726     result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
727     ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
728     ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
729
730     /* If the Windows directory is the root directory, it ends in backslash, not else. */
731     if (strlen(windowsdir) != 3) /* As in  "C:\"  or  "F:\"  */
732     {
733         strcat(windowsdir, "\\");
734     }
735
736     windowsdrive[0] = windowsdir[0];
737     windowsdrive[1] = windowsdir[1];
738     windowsdrive[2] = '\0';
739
740     result = GetTempFileNameA(windowsdrive, "abc", 1, out);
741     ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
742     ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
743        "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
744        windowsdrive[0], out);
745
746     result = GetTempFileNameA(windowsdir, "abc", 2, out);
747     ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
748     expected[0] = '\0';
749     strcat(expected, windowsdir);
750     strcat(expected, "abc2.tmp");
751     ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
752        out, expected);
753 }
754
755 static void test_DeleteFileA( void )
756 {
757     BOOL ret;
758
759     ret = DeleteFileA(NULL);
760     ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
761                 GetLastError() == ERROR_PATH_NOT_FOUND),
762        "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
763
764     ret = DeleteFileA("");
765     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
766                 GetLastError() == ERROR_BAD_PATHNAME),
767        "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
768
769     ret = DeleteFileA("nul");
770     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
771                 GetLastError() == ERROR_INVALID_PARAMETER ||
772                 GetLastError() == ERROR_ACCESS_DENIED ||
773                 GetLastError() == ERROR_INVALID_FUNCTION),
774        "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
775 }
776
777 static void test_DeleteFileW( void )
778 {
779     BOOL ret;
780     static const WCHAR emptyW[]={'\0'};
781
782     ret = DeleteFileW(NULL);
783     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
784         return;
785     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
786        "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
787
788     ret = DeleteFileW(emptyW);
789     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
790        "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
791 }
792
793 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
794
795 static void test_MoveFileA(void)
796 {
797     char tempdir[MAX_PATH];
798     char source[MAX_PATH], dest[MAX_PATH];
799     static const char prefix[] = "pfx";
800     DWORD ret;
801
802     ret = GetTempPathA(MAX_PATH, tempdir);
803     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
804     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
805
806     ret = GetTempFileNameA(tempdir, prefix, 0, source);
807     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
808
809     ret = GetTempFileNameA(tempdir, prefix, 0, dest);
810     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
811
812     ret = MoveFileA(source, dest);
813     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
814        "MoveFileA: unexpected error %d\n", GetLastError());
815
816     ret = DeleteFileA(dest);
817     ok(ret, "DeleteFileA: error %d\n", GetLastError());
818
819     ret = MoveFileA(source, dest);
820     ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
821
822     lstrcatA(tempdir, "Remove Me");
823     ret = CreateDirectoryA(tempdir, NULL);
824     ok(ret == TRUE, "CreateDirectoryA failed\n");
825
826     lstrcpyA(source, dest);
827     lstrcpyA(dest, tempdir);
828     lstrcatA(dest, "\\wild?.*");
829     /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
830     ret = MoveFileA(source, dest);
831     ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
832     ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
833        GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
834        "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
835     if (ret || (GetLastError() != ERROR_INVALID_NAME))
836     {
837         WIN32_FIND_DATAA fd;
838         char temppath[MAX_PATH];
839         HANDLE hFind;
840
841         lstrcpyA(temppath, tempdir);
842         lstrcatA(temppath, "\\*.*");
843         hFind = FindFirstFileA(temppath, &fd);
844         if (INVALID_HANDLE_VALUE != hFind)
845         {
846           LPSTR lpName;
847           do
848           {
849             lpName = fd.cAlternateFileName;
850             if (!lpName[0])
851               lpName = fd.cFileName;
852             ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
853           }
854           while (FindNextFileA(hFind, &fd));
855           FindClose(hFind);
856         }
857     }
858     ret = DeleteFileA(source);
859     ok(ret, "DeleteFileA: error %d\n", GetLastError());
860     ret = DeleteFileA(dest);
861     ok(!ret, "DeleteFileA: error %d\n", GetLastError());
862     ret = RemoveDirectoryA(tempdir);
863     ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
864 }
865
866 static void test_MoveFileW(void)
867 {
868     WCHAR temp_path[MAX_PATH];
869     WCHAR source[MAX_PATH], dest[MAX_PATH];
870     static const WCHAR prefix[] = {'p','f','x',0};
871     DWORD ret;
872
873     ret = GetTempPathW(MAX_PATH, temp_path);
874     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
875         return;
876     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
877     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
878
879     ret = GetTempFileNameW(temp_path, prefix, 0, source);
880     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
881
882     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
883     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
884
885     ret = MoveFileW(source, dest);
886     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
887        "CopyFileW: unexpected error %d\n", GetLastError());
888
889     ret = DeleteFileW(source);
890     ok(ret, "DeleteFileW: error %d\n", GetLastError());
891     ret = DeleteFileW(dest);
892     ok(ret, "DeleteFileW: error %d\n", GetLastError());
893 }
894
895 #define PATTERN_OFFSET 0x10
896
897 static void test_offset_in_overlapped_structure(void)
898 {
899     HANDLE hFile;
900     OVERLAPPED ov;
901     DWORD done, offset;
902     BOOL rc;
903     BYTE buf[256], pattern[] = "TeSt";
904     UINT i;
905     char temp_path[MAX_PATH], temp_fname[MAX_PATH];
906     BOOL ret;
907
908     ret =GetTempPathA(MAX_PATH, temp_path);
909     ok( ret, "GetTempPathA error %d\n", GetLastError());
910     ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
911     ok( ret, "GetTempFileNameA error %d\n", GetLastError());
912
913     /*** Write File *****************************************************/
914
915     hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
916     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
917
918     for(i = 0; i < sizeof(buf); i++) buf[i] = i;
919     ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
920     ok( ret, "WriteFile error %d\n", GetLastError());
921     ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
922
923     memset(&ov, 0, sizeof(ov));
924     S(U(ov)).Offset = PATTERN_OFFSET;
925     S(U(ov)).OffsetHigh = 0;
926     rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
927     /* Win 9x does not support the overlapped I/O on files */
928     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
929         ok(rc, "WriteFile error %d\n", GetLastError());
930         ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
931         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
932         ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
933
934         S(U(ov)).Offset = sizeof(buf) * 2;
935         S(U(ov)).OffsetHigh = 0;
936         ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
937         ok( ret, "WriteFile error %d\n", GetLastError());
938         ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
939         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
940         ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
941     }
942
943     CloseHandle(hFile);
944
945     /*** Read File *****************************************************/
946
947     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
948     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
949
950     memset(buf, 0, sizeof(buf));
951     memset(&ov, 0, sizeof(ov));
952     S(U(ov)).Offset = PATTERN_OFFSET;
953     S(U(ov)).OffsetHigh = 0;
954     rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
955     /* Win 9x does not support the overlapped I/O on files */
956     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
957         ok(rc, "ReadFile error %d\n", GetLastError());
958         ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
959         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
960         ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
961         ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
962     }
963
964     CloseHandle(hFile);
965
966     ret = DeleteFileA(temp_fname);
967     ok( ret, "DeleteFileA error %d\n", GetLastError());
968 }
969
970 static void test_LockFile(void)
971 {
972     HANDLE handle;
973     DWORD written;
974     OVERLAPPED overlapped;
975     int limited_LockFile;
976     int limited_UnLockFile;
977     int lockfileex_capable;
978
979     handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
980                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
981                           CREATE_ALWAYS, 0, 0 );
982     if (handle == INVALID_HANDLE_VALUE)
983     {
984         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
985         return;
986     }
987     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
988
989     ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
990     ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
991
992     limited_UnLockFile = 0;
993     if (UnlockFile( handle, 0, 0, 0, 0 ))
994     {
995         limited_UnLockFile = 1;
996     }
997
998     ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
999     /* overlapping locks must fail */
1000     ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1001     ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1002     /* non-overlapping locks must succeed */
1003     ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1004
1005     ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1006     ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1007     ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1008     ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1009
1010     S(U(overlapped)).Offset = 100;
1011     S(U(overlapped)).OffsetHigh = 0;
1012     overlapped.hEvent = 0;
1013
1014     lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1015     if (lockfileex_capable)
1016     {
1017         /* Test for broken LockFileEx a la Windows 95 OSR2. */
1018         if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1019         {
1020             /* LockFileEx is probably OK, test it more. */
1021             ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1022                 "LockFileEx 100,100 failed\n" );
1023         }
1024     }
1025
1026     /* overlapping shared locks are OK */
1027     S(U(overlapped)).Offset = 150;
1028     limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1029
1030     /* but exclusive is not */
1031     if (lockfileex_capable)
1032     {
1033         ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1034                          0, 50, 0, &overlapped ),
1035                          "LockFileEx exclusive 150,50 succeeded\n" );
1036         if (dll_capable("kernel32.dll", "UnlockFileEx"))
1037         {
1038             if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1039             { /* UnLockFile is capable. */
1040                 S(U(overlapped)).Offset = 100;
1041                 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1042                     "UnlockFileEx 150,100 again succeeded\n" );
1043             }
1044         }
1045     }
1046
1047     ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1048     ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1049     ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1050     ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1051
1052     /* wrap-around lock should not do anything */
1053     /* (but still succeeds on NT4 so we don't check result) */
1054     LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1055
1056     limited_LockFile = 0;
1057     if (!LockFile( handle, ~0, ~0, 1, 0 ))
1058     {
1059         limited_LockFile = 1;
1060     }
1061
1062     limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1063
1064     /* zero-byte lock */
1065     ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1066     limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1067     ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1068     limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1069
1070     ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1071     !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1072
1073     ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1074
1075     CloseHandle( handle );
1076     DeleteFileA( filename );
1077 }
1078
1079 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1080 {
1081     if (!is_win9x)
1082     {
1083         if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1084         if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1085     }
1086     else
1087     {
1088         access1 &= ~DELETE;
1089         if (!access1) access1 = GENERIC_READ;
1090
1091         access2 &= ~DELETE;
1092         if (!access2) access2 = GENERIC_READ;
1093     }
1094
1095     if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1096     if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1097     if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1098     if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1099     if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1100     if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1101     return 1;
1102 }
1103
1104 static void test_file_sharing(void)
1105 {
1106     static const DWORD access_modes[] =
1107         { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1108           DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1109     static const DWORD sharing_modes[] =
1110         { 0, FILE_SHARE_READ,
1111           FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1112           FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1113           FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1114     int a1, s1, a2, s2;
1115     int ret;
1116     HANDLE h, h2;
1117     BOOL is_win9x = FALSE;
1118
1119     /* make sure the file exists */
1120     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1121     if (h == INVALID_HANDLE_VALUE)
1122     {
1123         ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1124         return;
1125     }
1126     is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1127     CloseHandle( h );
1128
1129     for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1130     {
1131         for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1132         {
1133             /* Win9x doesn't support FILE_SHARE_DELETE */
1134             if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1135                 continue;
1136
1137             SetLastError(0xdeadbeef);
1138             h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1139                              NULL, OPEN_EXISTING, 0, 0 );
1140             if (h == INVALID_HANDLE_VALUE)
1141             {
1142                 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1143                 return;
1144             }
1145             for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1146             {
1147                 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1148                 {
1149                     /* Win9x doesn't support FILE_SHARE_DELETE */
1150                     if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1151                         continue;
1152
1153                     SetLastError(0xdeadbeef);
1154                     h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1155                                       NULL, OPEN_EXISTING, 0, 0 );
1156
1157                     if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1158                                                access_modes[a2], sharing_modes[s2], is_win9x ))
1159                     {
1160                         ret = GetLastError();
1161
1162                         ok( h2 != INVALID_HANDLE_VALUE,
1163                             "open failed for modes %x/%x/%x/%x\n",
1164                             access_modes[a1], sharing_modes[s1],
1165                             access_modes[a2], sharing_modes[s2] );
1166                         ok( ret == 0xdeadbeef /* Win9x */ ||
1167                             ret == 0, /* XP */
1168                              "wrong error code %d\n", ret );
1169
1170                         CloseHandle( h2 );
1171                     }
1172                     else
1173                     {
1174                         ret = GetLastError();
1175
1176                         ok( h2 == INVALID_HANDLE_VALUE,
1177                             "open succeeded for modes %x/%x/%x/%x\n",
1178                             access_modes[a1], sharing_modes[s1],
1179                             access_modes[a2], sharing_modes[s2] );
1180                          ok( ret == ERROR_SHARING_VIOLATION,
1181                              "wrong error code %d\n", ret );
1182                     }
1183                 }
1184             }
1185             CloseHandle( h );
1186         }
1187     }
1188
1189     SetLastError(0xdeadbeef);
1190     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1191     ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1192
1193     SetLastError(0xdeadbeef);
1194     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1195     ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1196     ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1197
1198     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1199     ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1200
1201     CloseHandle(h);
1202     CloseHandle(h2);
1203
1204     DeleteFileA( filename );
1205 }
1206
1207 static char get_windows_drive(void)
1208 {
1209     char windowsdir[MAX_PATH];
1210     GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1211     return windowsdir[0];
1212 }
1213
1214 static void test_FindFirstFileA(void)
1215 {
1216     HANDLE handle;
1217     WIN32_FIND_DATAA data;
1218     int err;
1219     char buffer[5] = "C:\\";
1220     char buffer2[100];
1221
1222     /* try FindFirstFileA on "C:\" */
1223     buffer[0] = get_windows_drive();
1224     
1225     SetLastError( 0xdeadbeaf );
1226     handle = FindFirstFileA(buffer, &data);
1227     err = GetLastError();
1228     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1229     ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1230
1231     /* try FindFirstFileA on "C:\*" */
1232     strcpy(buffer2, buffer);
1233     strcat(buffer2, "*");
1234     handle = FindFirstFileA(buffer2, &data);
1235     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1236     ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1237          "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1238     if (FindNextFileA( handle, &data ))
1239         ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1240              "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1241     ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1242
1243     /* try FindFirstFileA on windows dir */
1244     GetWindowsDirectory( buffer2, sizeof(buffer2) );
1245     strcat(buffer2, "\\*");
1246     handle = FindFirstFileA(buffer2, &data);
1247     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1248     ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1249     ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1250     ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1251     while (FindNextFileA( handle, &data ))
1252         ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1253              "FindNextFile shouldn't return '%s'\n", data.cFileName );
1254     ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1255
1256     /* try FindFirstFileA on "C:\foo\" */
1257     SetLastError( 0xdeadbeaf );
1258     strcpy(buffer2, buffer);
1259     strcat(buffer2, "foo\\");
1260     handle = FindFirstFileA(buffer2, &data);
1261     err = GetLastError();
1262     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1263     todo_wine {
1264         ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1265     }
1266
1267     /* try FindFirstFileA on "C:\foo\bar.txt" */
1268     SetLastError( 0xdeadbeaf );
1269     strcpy(buffer2, buffer);
1270     strcat(buffer2, "foo\\bar.txt");
1271     handle = FindFirstFileA(buffer2, &data);
1272     err = GetLastError();
1273     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1274     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1275
1276     /* try FindFirstFileA on "C:\foo\*.*" */
1277     SetLastError( 0xdeadbeaf );
1278     strcpy(buffer2, buffer);
1279     strcat(buffer2, "foo\\*.*");
1280     handle = FindFirstFileA(buffer2, &data);
1281     err = GetLastError();
1282     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1283     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1284
1285     /* try FindFirstFileA on "foo\bar.txt" */
1286     SetLastError( 0xdeadbeaf );
1287     strcpy(buffer2, "foo\\bar.txt");
1288     handle = FindFirstFileA(buffer2, &data);
1289     err = GetLastError();
1290     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1291     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1292
1293     /* try FindFirstFileA on "c:\nul" */
1294     SetLastError( 0xdeadbeaf );
1295     strcpy(buffer2, buffer);
1296     strcat(buffer2, "nul");
1297     handle = FindFirstFileA(buffer2, &data);
1298     err = GetLastError();
1299     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1300     ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1301     ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1302     ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1303     ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1304     SetLastError( 0xdeadbeaf );
1305     ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1306     ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1307     ok( FindClose( handle ), "failed to close handle\n" );
1308
1309     /* try FindFirstFileA on "lpt1" */
1310     SetLastError( 0xdeadbeaf );
1311     strcpy(buffer2, "lpt1");
1312     handle = FindFirstFileA(buffer2, &data);
1313     err = GetLastError();
1314     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1315     ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1316     ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1317     ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1318     ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes, "wrong attributes %x\n", data.dwFileAttributes );
1319     SetLastError( 0xdeadbeaf );
1320     ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1321     ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1322     ok( FindClose( handle ), "failed to close handle\n" );
1323
1324     /* try FindFirstFileA on "c:\nul\*" */
1325     SetLastError( 0xdeadbeaf );
1326     strcpy(buffer2, buffer);
1327     strcat(buffer2, "nul\\*");
1328     handle = FindFirstFileA(buffer2, &data);
1329     err = GetLastError();
1330     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1331     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1332
1333     /* try FindFirstFileA on "c:\nul*" */
1334     SetLastError( 0xdeadbeaf );
1335     strcpy(buffer2, buffer);
1336     strcat(buffer2, "nul*");
1337     handle = FindFirstFileA(buffer2, &data);
1338     err = GetLastError();
1339     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1340     ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1341
1342     /* try FindFirstFileA on "c:\foo\bar\nul" */
1343     SetLastError( 0xdeadbeaf );
1344     strcpy(buffer2, buffer);
1345     strcat(buffer2, "foo\\bar\\nul");
1346     handle = FindFirstFileA(buffer2, &data);
1347     err = GetLastError();
1348     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1349     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1350
1351     /* try FindFirstFileA on "c:\foo\nul\bar" */
1352     SetLastError( 0xdeadbeaf );
1353     strcpy(buffer2, buffer);
1354     strcat(buffer2, "foo\\nul\\bar");
1355     handle = FindFirstFileA(buffer2, &data);
1356     err = GetLastError();
1357     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1358     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1359 }
1360
1361 static void test_FindNextFileA(void)
1362 {
1363     HANDLE handle;
1364     WIN32_FIND_DATAA search_results;
1365     int err;
1366     char buffer[5] = "C:\\*";
1367
1368     buffer[0] = get_windows_drive();
1369     handle = FindFirstFileA(buffer,&search_results);
1370     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1371     while (FindNextFile(handle, &search_results))
1372     {
1373         /* get to the end of the files */
1374     }
1375     ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1376     err = GetLastError();
1377     ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1378 }
1379
1380 static int test_Mapfile_createtemp(HANDLE *handle)
1381 {
1382     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1383     DeleteFile(filename);
1384     *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1385                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1386     if (*handle != INVALID_HANDLE_VALUE) {
1387
1388         return 1;
1389     }
1390
1391     return 0;
1392 }
1393
1394 static void test_MapFile(void)
1395 {
1396     HANDLE handle;
1397     HANDLE hmap;
1398
1399     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1400
1401     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1402     ok( hmap != NULL, "mapping should work, I named it!\n" );
1403
1404     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1405
1406     /* We have to close file before we try new stuff with mapping again.
1407        Else we would always succeed on XP or block descriptors on 95. */
1408     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1409     ok( hmap != NULL, "We should still be able to map!\n" );
1410     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1411     ok( CloseHandle( handle ), "can't close file handle\n");
1412     handle = NULL;
1413
1414     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1415
1416     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1417     ok( hmap == NULL, "mapped zero size file\n");
1418     ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1419
1420     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1421     ok( hmap == NULL, "mapping should fail\n");
1422     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1423
1424     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1425     ok( hmap == NULL, "mapping should fail\n");
1426     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1427
1428     /* On XP you can now map again, on Win 95 you cannot. */
1429
1430     ok( CloseHandle( handle ), "can't close file handle\n");
1431     ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1432 }
1433
1434 static void test_GetFileType(void)
1435 {
1436     DWORD type;
1437     HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1438     ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1439     type = GetFileType(h);
1440     ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1441     CloseHandle( h );
1442     h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1443     ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1444     type = GetFileType(h);
1445     ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1446     CloseHandle( h );
1447     DeleteFileA( filename );
1448 }
1449
1450 static int completion_count;
1451
1452 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1453 {
1454 /*      printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1455         ReleaseSemaphore(ovl->hEvent, 1, NULL);
1456         completion_count++;
1457 }
1458
1459 static void test_async_file_errors(void)
1460 {
1461     char szFile[MAX_PATH];
1462     HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1463     HANDLE hFile;
1464     LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1465     OVERLAPPED ovl;
1466     S(U(ovl)).Offset = 0;
1467     S(U(ovl)).OffsetHigh = 0;
1468     ovl.hEvent = hSem;
1469     completion_count = 0;
1470     szFile[0] = '\0';
1471     GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1472     strcat(szFile, "\\win.ini");
1473     hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1474     ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1475     while (TRUE)
1476     {
1477         BOOL res;
1478         while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1479             ;
1480         res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1481         /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1482         if (!res)
1483             break;
1484         S(U(ovl)).Offset += 4096;
1485         /* i/o completion routine only called if ReadFileEx returned success.
1486          * we only care about violations of this rule so undo what should have
1487          * been done */
1488         completion_count--;
1489     }
1490     ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1491     /*printf("Error = %ld\n", GetLastError());*/
1492 }
1493
1494 static void test_read_write(void)
1495 {
1496     DWORD bytes, ret;
1497     HANDLE hFile;
1498     char temp_path[MAX_PATH];
1499     char filename[MAX_PATH];
1500     static const char prefix[] = "pfx";
1501
1502     ret = GetTempPathA(MAX_PATH, temp_path);
1503     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1504     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1505
1506     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1507     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1508
1509     hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1510                         CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1511     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
1512
1513     SetLastError(12345678);
1514     bytes = 12345678;
1515     ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1516     ok(ret && GetLastError() == 12345678,
1517         "ret = %d, error %d\n", ret, GetLastError());
1518     ok(!bytes, "bytes = %d\n", bytes);
1519
1520     SetLastError(12345678);
1521     bytes = 12345678;
1522     ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1523     ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1524         (ret && GetLastError() == 12345678), /* Win9x */
1525         "ret = %d, error %d\n", ret, GetLastError());
1526     ok(!bytes || /* Win2k */
1527         bytes == 10, /* Win9x */
1528         "bytes = %d\n", bytes);
1529
1530     /* make sure the file contains data */
1531     WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
1532     SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1533
1534     SetLastError(12345678);
1535     bytes = 12345678;
1536     ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1537     ok(ret && GetLastError() == 12345678,
1538         "ret = %d, error %d\n", ret, GetLastError());
1539     ok(!bytes, "bytes = %d\n", bytes);
1540
1541     SetLastError(12345678);
1542     bytes = 12345678;
1543     ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1544     ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1545                 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1546         "ret = %d, error %d\n", ret, GetLastError());
1547     ok(!bytes, "bytes = %d\n", bytes);
1548
1549     ret = CloseHandle(hFile);
1550     ok( ret, "CloseHandle: error %d\n", GetLastError());
1551     ret = DeleteFileA(filename);
1552     ok( ret, "DeleteFileA: error %d\n", GetLastError());
1553 }
1554
1555 static void test_OpenFile(void)
1556 {
1557     HFILE hFile;
1558     OFSTRUCT ofs;
1559     BOOL ret;
1560     DWORD retval;
1561     
1562     static const char *file = "\\regsvr32.exe";
1563     static const char *foo = ".\\foo-bar-foo.baz";
1564     static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
1565         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1566         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1567         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1568         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1569         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1570     static const char *backslash = "\\";
1571     char buff[MAX_PATH];
1572     char buff_long[4*MAX_PATH];
1573     char filled_0xA5[OFS_MAXPATHNAME];
1574     UINT length;
1575     
1576     /* Check for existing file */
1577     length = GetSystemDirectoryA(buff, MAX_PATH);
1578
1579     if (length + lstrlen(file) < MAX_PATH)
1580     {
1581         lstrcatA(buff, file);
1582         memset(&ofs, 0xA5, sizeof(ofs));
1583         SetLastError(0xfaceabee);
1584
1585         hFile = OpenFile(buff, &ofs, OF_EXIST);
1586         ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
1587         ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1588             "GetLastError() returns %d\n", GetLastError() );
1589         ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1590         ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1591         ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1592             "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
1593             ofs.szPathName, buff );
1594     }
1595
1596     memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
1597     length = GetCurrentDirectoryA(MAX_PATH, buff);
1598
1599     /* Check for nonexistent file */
1600     if (length + lstrlenA(foo + 1) < MAX_PATH)
1601     {
1602         lstrcatA(buff, foo + 1); /* Avoid '.' during concatenation */
1603         memset(&ofs, 0xA5, sizeof(ofs));
1604         SetLastError(0xfaceabee);
1605
1606         hFile = OpenFile(foo, &ofs, OF_EXIST);
1607         ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1608         ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
1609         todo_wine
1610         ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1611         ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1612         ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
1613             "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n", 
1614             ofs.szPathName, buff );
1615     }
1616
1617     length = GetCurrentDirectoryA(MAX_PATH, buff_long);
1618     length += lstrlenA(foo_too_long + 1);
1619
1620     /* Check for nonexistent file with too long filename */ 
1621     if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long)) 
1622     {
1623         lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
1624         memset(&ofs, 0xA5, sizeof(ofs));
1625         SetLastError(0xfaceabee);
1626
1627         hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
1628         ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
1629         ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE, 
1630             "GetLastError() returns %d\n", GetLastError() );
1631         todo_wine
1632         ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1633         ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
1634             "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1635         ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0, 
1636             "OpenFile returned '%s', but was expected to return string filled with 0xA5\n", 
1637             ofs.szPathName );
1638     }
1639
1640     length = GetCurrentDirectoryA(MAX_PATH, buff);
1641     length += lstrlenA(backslash);
1642     length += lstrlenA(filename);
1643
1644     if (length >= MAX_PATH) 
1645     {
1646         trace("Buffer too small, requested length = %d, but MAX_PATH = %d.  Skipping test.\n", length, MAX_PATH);
1647         return;
1648     }
1649     lstrcatA(buff, backslash);
1650     lstrcatA(buff, filename);
1651
1652     memset(&ofs, 0xA5, sizeof(ofs));
1653     SetLastError(0xfaceabee);
1654     /* Create an empty file */
1655     hFile = OpenFile(filename, &ofs, OF_CREATE);
1656     ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
1657     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1658         "GetLastError() returns %d\n", GetLastError() );
1659     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1660     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1661     ret = CloseHandle((HANDLE)hFile);
1662     ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1663     retval = GetFileAttributesA(filename);
1664     ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
1665
1666     memset(&ofs, 0xA5, sizeof(ofs));
1667     SetLastError(0xfaceabee);
1668     /* Check various opening options: */
1669     /* for reading only, */
1670     hFile = OpenFile(filename, &ofs, OF_READ);
1671     ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
1672     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1673         "GetLastError() returns %d\n", GetLastError() );
1674     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1675     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1676     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1677         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1678     ret = CloseHandle((HANDLE)hFile);
1679     ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1680
1681     memset(&ofs, 0xA5, sizeof(ofs));
1682     SetLastError(0xfaceabee);
1683     /* for writing only, */
1684     hFile = OpenFile(filename, &ofs, OF_WRITE);
1685     ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
1686     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1687         "GetLastError() returns %d\n", GetLastError() );
1688     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1689     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1690     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1691         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1692     ret = CloseHandle((HANDLE)hFile);
1693     ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1694
1695     memset(&ofs, 0xA5, sizeof(ofs));
1696     SetLastError(0xfaceabee);
1697     /* for reading and writing, */
1698     hFile = OpenFile(filename, &ofs, OF_READWRITE);
1699     ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
1700     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1701         "GetLastError() returns %d\n", GetLastError() );
1702     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1703     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1704     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1705         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1706     ret = CloseHandle((HANDLE)hFile);
1707     ok( ret == TRUE, "CloseHandle() returns %d\n", ret );
1708
1709     memset(&ofs, 0xA5, sizeof(ofs));
1710     SetLastError(0xfaceabee);
1711     /* for checking file presence. */
1712     hFile = OpenFile(filename, &ofs, OF_EXIST);
1713     ok( hFile == 1, "OpenFile failed on finding our created file\n" );
1714     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1715         "GetLastError() returns %d\n", GetLastError() );
1716     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1717     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1718     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1719         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1720
1721     memset(&ofs, 0xA5, sizeof(ofs));
1722     SetLastError(0xfaceabee);
1723     /* Delete the file and make sure it doesn't exist anymore */
1724     hFile = OpenFile(filename, &ofs, OF_DELETE);
1725     ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
1726     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
1727         "GetLastError() returns %d\n", GetLastError() );
1728     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
1729     ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
1730     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
1731         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
1732
1733     retval = GetFileAttributesA(filename);
1734     ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
1735 }
1736
1737 static void test_overlapped(void)
1738 {
1739     OVERLAPPED ov;
1740     DWORD r, result;
1741
1742     /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
1743
1744     memset( &ov, 0,  sizeof ov );
1745     result = 1;
1746     r = GetOverlappedResult(0, &ov, &result, 0);
1747     ok( r == TRUE, "should return false\n");
1748     ok( result == 0, "wrong result %u\n", result );
1749
1750     result = 0;
1751     ov.Internal = 0;
1752     ov.InternalHigh = 0xabcd;
1753     r = GetOverlappedResult(0, &ov, &result, 0);
1754     ok( r == TRUE, "should return false\n");
1755     ok( result == 0xabcd, "wrong result %u\n", result );
1756
1757     SetLastError( 0xb00 );
1758     result = 0;
1759     ov.Internal = STATUS_INVALID_HANDLE;
1760     ov.InternalHigh = 0xabcd;
1761     r = GetOverlappedResult(0, &ov, &result, 0);
1762     ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1763     ok( r == FALSE, "should return false\n");
1764     ok( result == 0xabcd, "wrong result %u\n", result );
1765
1766     SetLastError( 0xb00 );
1767     result = 0;
1768     ov.Internal = STATUS_PENDING;
1769     ov.InternalHigh = 0xabcd;
1770     r = GetOverlappedResult(0, &ov, &result, 0);
1771     todo_wine {
1772     ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1773     }
1774     ok( r == FALSE, "should return false\n");
1775     ok( result == 0, "wrong result %u\n", result );
1776
1777     SetLastError( 0xb00 );
1778     ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
1779     ov.Internal = STATUS_PENDING;
1780     ov.InternalHigh = 0xabcd;
1781     r = GetOverlappedResult(0, &ov, &result, 0);
1782     ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1783     ok( r == FALSE, "should return false\n");
1784
1785     ResetEvent( ov.hEvent );
1786
1787     SetLastError( 0xb00 );
1788     ov.Internal = STATUS_PENDING;
1789     ov.InternalHigh = 0;
1790     r = GetOverlappedResult(0, &ov, &result, 0);
1791     ok( GetLastError() == ERROR_IO_INCOMPLETE, "wrong error %u\n", GetLastError() );
1792     ok( r == FALSE, "should return false\n");
1793
1794     r = CloseHandle( ov.hEvent );
1795     ok( r == TRUE, "close handle failed\n");
1796 }
1797
1798 START_TEST(file)
1799 {
1800     test__hread(  );
1801     test__hwrite(  );
1802     test__lclose(  );
1803     test__lcreat(  );
1804     test__llseek(  );
1805     test__llopen(  );
1806     test__lread(  );
1807     test__lwrite(  );
1808     test_GetTempFileNameA();
1809     test_CopyFileA();
1810     test_CopyFileW();
1811     test_CreateFileA();
1812     test_CreateFileW();
1813     test_DeleteFileA();
1814     test_DeleteFileW();
1815     test_MoveFileA();
1816     test_MoveFileW();
1817     test_FindFirstFileA();
1818     test_FindNextFileA();
1819     test_LockFile();
1820     test_file_sharing();
1821     test_offset_in_overlapped_structure();
1822     test_MapFile();
1823     test_GetFileType();
1824     test_async_file_errors();
1825     test_read_write();
1826     test_OpenFile();
1827     test_overlapped();
1828 }