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