kernel32/tests: Fix some test failures on WinMe.
[wine] / dlls / kernel32 / tests / file.c
1 /*
2  * Unit tests for file functions in Wine
3  *
4  * Copyright (c) 2002, 2004 Jakob Eriksson
5  * Copyright (c) 2008 Jeff Zaroyko
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <stdio.h>
30
31 #include "wine/test.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35
36 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
37 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
38 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
39 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
40 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
41
42 /* keep filename and filenameW the same */
43 static const char filename[] = "testfile.xxx";
44 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
45 static const char sillytext[] =
46 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
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 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
53 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
55 "sdlkfjasdlkfj a dslkj adsklf  \n  \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
56
57 struct test_list {
58     const char *file;           /* file string to test */
59     const DWORD err;            /* Win NT and further error code */
60     const LONG err2;            /* Win 9x & ME error code  or -1 */
61     const DWORD options;        /* option flag to use for open */
62     const BOOL todo_flag;       /* todo_wine indicator */
63 } ;
64
65 static void InitFunctionPointers(void)
66 {
67     HMODULE hkernel32 = GetModuleHandleA("kernel32");
68
69     pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
70     pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
71     pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
72     pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
73     pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
74 }
75
76 static void test__hread( void )
77 {
78     HFILE filehandle;
79     char buffer[10000];
80     long bytes_read;
81     long bytes_wanted;
82     long i;
83     BOOL ret;
84
85     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
86     DeleteFileA( filename );
87     filehandle = _lcreat( filename, 0 );
88     if (filehandle == HFILE_ERROR)
89     {
90         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
91         return;
92     }
93
94     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
95
96     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
97
98     filehandle = _lopen( filename, OF_READ );
99
100     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError(  ) );
101
102     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
103
104     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
105
106     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
107     {
108         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
109         ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
110         for (i = 0; i < bytes_wanted; i++)
111         {
112             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
113         }
114     }
115
116     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
117
118     ret = DeleteFileA( filename );
119     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError(  ) );
120 }
121
122
123 static void test__hwrite( void )
124 {
125     HFILE filehandle;
126     char buffer[10000];
127     long bytes_read;
128     long bytes_written;
129     long blocks;
130     long i;
131     char *contents;
132     HLOCAL memory_object;
133     char checksum[1];
134     BOOL ret;
135
136     filehandle = _lcreat( filename, 0 );
137     if (filehandle == HFILE_ERROR)
138     {
139         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
140         return;
141     }
142
143     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
144
145     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
146
147     filehandle = _lopen( filename, OF_READ );
148
149     bytes_read = _hread( filehandle, buffer, 1);
150
151     ok( 0 == bytes_read, "file read size error\n" );
152
153     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
154
155     filehandle = _lopen( filename, OF_READWRITE );
156
157     bytes_written = 0;
158     checksum[0] = '\0';
159     srand( (unsigned)time( NULL ) );
160     for (blocks = 0; blocks < 100; blocks++)
161     {
162         for (i = 0; i < (long)sizeof( buffer ); i++)
163         {
164             buffer[i] = rand(  );
165             checksum[0] = checksum[0] + buffer[i];
166         }
167         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
168         bytes_written = bytes_written + sizeof( buffer );
169     }
170
171     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
172     bytes_written++;
173
174     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
175
176     memory_object = LocalAlloc( LPTR, bytes_written );
177
178     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
179
180     contents = LocalLock( memory_object );
181
182     filehandle = _lopen( filename, OF_READ );
183
184     contents = LocalLock( memory_object );
185
186     ok( NULL != contents, "LocalLock whines\n" );
187
188     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
189
190     checksum[0] = '\0';
191     i = 0;
192     do
193     {
194         checksum[0] = checksum[0] + contents[i];
195         i++;
196     }
197     while (i < bytes_written - 1);
198
199     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\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__lclose( void )
209 {
210     HFILE filehandle;
211     BOOL ret;
212
213     filehandle = _lcreat( filename, 0 );
214     if (filehandle == HFILE_ERROR)
215     {
216         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
217         return;
218     }
219
220     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
221
222     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
223
224     ret = DeleteFileA( filename );
225     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError(  ) );
226 }
227
228
229 static void test__lcreat( void )
230 {
231     HFILE filehandle;
232     char buffer[10000];
233     WIN32_FIND_DATAA search_results;
234     char slashname[] = "testfi/";
235     int err;
236     HANDLE find;
237     BOOL ret;
238
239     filehandle = _lcreat( filename, 0 );
240     if (filehandle == HFILE_ERROR)
241     {
242         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
243         return;
244     }
245
246     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
247
248     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
249
250     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
251
252     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
253
254     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
255
256     ret = DeleteFileA(filename);
257     ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
258
259     filehandle = _lcreat( filename, 1 ); /* readonly */
260     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
261
262     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
263
264     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
265
266     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
267
268     ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
269
270     ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
271
272     ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
273
274     filehandle = _lcreat( filename, 2 );
275     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
276
277     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
278
279     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
280
281     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
282
283     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
284
285     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
286
287     ret = DeleteFileA( filename );
288     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
289
290     filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
291     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError(  ) );
292
293     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
294
295     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
296
297     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
298
299     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
300
301     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
302
303     ret = DeleteFileA( filename );
304     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
305
306     filehandle=_lcreat (slashname, 0); /* illegal name */
307     if (HFILE_ERROR==filehandle) {
308       err=GetLastError ();
309       ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
310           "creating file \"%s\" failed with error %d\n", slashname, err);
311     } else { /* only NT succeeds */
312       _lclose(filehandle);
313       find=FindFirstFileA (slashname, &search_results);
314       if (INVALID_HANDLE_VALUE!=find)
315       {
316         ret = FindClose (find);
317         ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
318         slashname[strlen(slashname)-1]=0;
319         ok (!strcmp (slashname, 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( slashname );
326     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
327     }
328
329     filehandle=_lcreat (filename, 8); /* illegal attribute */
330     if (HFILE_ERROR==filehandle)
331       ok (0, "couldn't create volume label \"%s\"\n", filename);
332     else {
333       _lclose(filehandle);
334       find=FindFirstFileA (filename, &search_results);
335       if (INVALID_HANDLE_VALUE==find)
336         ok (0, "file \"%s\" not found\n", filename);
337       else {
338         ret = FindClose(find);
339         ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
340         ok (!strcmp (filename, search_results.cFileName),
341             "found unexpected name \"%s\"\n", search_results.cFileName);
342         search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
343         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
344             "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
345             search_results.dwFileAttributes);
346       }
347     ret = DeleteFileA( filename );
348     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
349     }
350 }
351
352
353 static void test__llseek( void )
354 {
355     INT i;
356     HFILE filehandle;
357     char buffer[1];
358     long bytes_read;
359     BOOL ret;
360
361     filehandle = _lcreat( filename, 0 );
362     if (filehandle == HFILE_ERROR)
363     {
364         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
365         return;
366     }
367
368     for (i = 0; i < 400; i++)
369     {
370         ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
371     }
372     ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
373     ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
374
375     bytes_read = _hread( filehandle, buffer, 1);
376     ok( 1 == bytes_read, "file read size error\n" );
377     ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
378     ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
379
380     bytes_read = _hread( filehandle, buffer, 1);
381     ok( 1 == bytes_read, "file read size error\n" );
382     ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
383     ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
384     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
385
386     ret = DeleteFileA( filename );
387     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
388 }
389
390
391 static void test__llopen( void )
392 {
393     HFILE filehandle;
394     UINT bytes_read;
395     char buffer[10000];
396     BOOL ret;
397
398     filehandle = _lcreat( filename, 0 );
399     if (filehandle == HFILE_ERROR)
400     {
401         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
402         return;
403     }
404
405     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
406     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
407
408     filehandle = _lopen( filename, OF_READ );
409     ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
410     bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
411     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
412     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
413
414     filehandle = _lopen( filename, OF_READWRITE );
415     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
416     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
417     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
418     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
419
420     filehandle = _lopen( filename, OF_WRITE );
421     ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
422     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
423     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
424
425     ret = DeleteFileA( filename );
426     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
427     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
428 }
429
430
431 static void test__lread( void )
432 {
433     HFILE filehandle;
434     char buffer[10000];
435     long bytes_read;
436     UINT bytes_wanted;
437     UINT i;
438     BOOL ret;
439
440     filehandle = _lcreat( filename, 0 );
441     if (filehandle == HFILE_ERROR)
442     {
443         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
444         return;
445     }
446
447     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
448
449     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
450
451     filehandle = _lopen( filename, OF_READ );
452
453     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
454
455     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
456
457     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
458
459     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
460     {
461         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
462         ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
463         for (i = 0; i < bytes_wanted; i++)
464         {
465             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
466         }
467     }
468
469     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
470
471     ret = DeleteFileA( filename );
472     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
473 }
474
475
476 static void test__lwrite( void )
477 {
478     HFILE filehandle;
479     char buffer[10000];
480     long bytes_read;
481     long bytes_written;
482     long blocks;
483     long i;
484     char *contents;
485     HLOCAL memory_object;
486     char checksum[1];
487     BOOL ret;
488
489     filehandle = _lcreat( filename, 0 );
490     if (filehandle == HFILE_ERROR)
491     {
492         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
493         return;
494     }
495
496     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
497
498     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
499
500     filehandle = _lopen( filename, OF_READ );
501
502     bytes_read = _hread( filehandle, buffer, 1);
503
504     ok( 0 == bytes_read, "file read size error\n" );
505
506     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
507
508     filehandle = _lopen( filename, OF_READWRITE );
509
510     bytes_written = 0;
511     checksum[0] = '\0';
512     srand( (unsigned)time( NULL ) );
513     for (blocks = 0; blocks < 100; blocks++)
514     {
515         for (i = 0; i < (long)sizeof( buffer ); i++)
516         {
517             buffer[i] = rand(  );
518             checksum[0] = checksum[0] + buffer[i];
519         }
520         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
521         bytes_written = bytes_written + sizeof( buffer );
522     }
523
524     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
525     bytes_written++;
526
527     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
528
529     memory_object = LocalAlloc( LPTR, bytes_written );
530
531     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
532
533     contents = LocalLock( memory_object );
534
535     filehandle = _lopen( filename, OF_READ );
536
537     contents = LocalLock( memory_object );
538
539     ok( NULL != contents, "LocalLock whines\n" );
540
541     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
542
543     checksum[0] = '\0';
544     i = 0;
545     do
546     {
547         checksum[0] += contents[i];
548         i++;
549     }
550     while (i < bytes_written - 1);
551
552     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
553
554     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
555
556     ret = DeleteFileA( filename );
557     ok( ret, "DeleteFile failed (%d)\n", GetLastError(  ) );
558 }
559
560 static void test_CopyFileA(void)
561 {
562     char temp_path[MAX_PATH];
563     char source[MAX_PATH], dest[MAX_PATH];
564     static const char prefix[] = "pfx";
565     HANDLE hfile;
566     FILETIME ft1, ft2;
567     char buf[10];
568     DWORD ret;
569     BOOL retok;
570
571     ret = GetTempPathA(MAX_PATH, temp_path);
572     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
573     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
574
575     ret = GetTempFileNameA(temp_path, prefix, 0, source);
576     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
577
578     ret = MoveFileA(source, source);
579     todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
580
581     /* make the source have not zero size */
582     hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
583     ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
584     retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
585     ok( retok && ret == sizeof(prefix),
586        "WriteFile error %d\n", GetLastError());
587     ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
588     /* get the file time and change it to prove the difference */
589     ret = GetFileTime(hfile, NULL, NULL, &ft1);
590     ok( ret, "GetFileTime error %d\n", GetLastError());
591     ft1.dwLowDateTime -= 600000000; /* 60 second */
592     ret = SetFileTime(hfile, NULL, NULL, &ft1);
593     ok( ret, "SetFileTime error %d\n", GetLastError());
594     GetFileTime(hfile, NULL, NULL, &ft1);  /* get the actual time back */
595     CloseHandle(hfile);
596
597     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
598     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
599
600     SetLastError(0xdeadbeef);
601     ret = CopyFileA(source, dest, TRUE);
602     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
603        "CopyFileA: unexpected error %d\n", GetLastError());
604
605     ret = CopyFileA(source, dest, FALSE);
606     ok(ret, "CopyFileA: error %d\n", GetLastError());
607
608     /* make sure that destination has correct size */
609     hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
610     ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
611     ret = GetFileSize(hfile, NULL);
612     ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
613
614     /* make sure that destination has the same filetime */
615     ret = GetFileTime(hfile, NULL, NULL, &ft2);
616     ok( ret, "GetFileTime error %d\n", GetLastError());
617     ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
618
619     SetLastError(0xdeadbeef);
620     ret = CopyFileA(source, dest, FALSE);
621     ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
622        "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
623
624     /* make sure that destination still has correct size */
625     ret = GetFileSize(hfile, NULL);
626     ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
627     retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
628     ok( retok && ret == sizeof(prefix),
629        "ReadFile: error %d\n", GetLastError());
630     ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
631     CloseHandle(hfile);
632
633     ret = DeleteFileA(source);
634     ok(ret, "DeleteFileA: error %d\n", GetLastError());
635     ret = DeleteFileA(dest);
636     ok(ret, "DeleteFileA: error %d\n", GetLastError());
637 }
638
639 static void test_CopyFileW(void)
640 {
641     WCHAR temp_path[MAX_PATH];
642     WCHAR source[MAX_PATH], dest[MAX_PATH];
643     static const WCHAR prefix[] = {'p','f','x',0};
644     DWORD ret;
645
646     ret = GetTempPathW(MAX_PATH, temp_path);
647     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
648     {
649         win_skip("GetTempPathW is not available\n");
650         return;
651     }
652     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
653     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
654
655     ret = GetTempFileNameW(temp_path, prefix, 0, source);
656     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
657
658     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
659     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
660
661     ret = CopyFileW(source, dest, TRUE);
662     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
663        "CopyFileW: unexpected error %d\n", GetLastError());
664
665     ret = CopyFileW(source, dest, FALSE);
666     ok(ret, "CopyFileW: error %d\n", GetLastError());
667
668     ret = DeleteFileW(source);
669     ok(ret, "DeleteFileW: error %d\n", GetLastError());
670     ret = DeleteFileW(dest);
671     ok(ret, "DeleteFileW: error %d\n", GetLastError());
672 }
673
674
675 /*
676  *   Debugging routine to dump a buffer in a hexdump-like fashion.
677  */
678 static void dumpmem(unsigned char* mem, int len) {
679     int x,y;
680     char buf[200];
681     int ln=0;
682
683     for (x=0; x<len; x+=16) {
684         ln += sprintf(buf+ln, "%04x: ",x);
685         for (y=0; y<16; y++) {
686             if ((x+y)>len) {
687                 ln += sprintf(buf+ln, "   ");
688             } else {
689                 ln += sprintf(buf+ln, "%02hhx ",mem[x+y]);
690             }
691         }
692         ln += sprintf(buf+ln, "- ");
693         for (y=0; y<16; y++) {
694             if ((x+y)<=len) {
695                 if (mem[x+y]<32 || mem[x+y]>127) {
696                     ln += sprintf(buf+ln, ".");
697                 } else {
698                     ln += sprintf(buf+ln, "%c",mem[x+y]);
699                 }
700             }
701         }
702         sprintf(buf+ln, "\n");
703         trace(buf);
704         ln = 0;
705     }
706 }
707
708 static void test_CreateFileA(void)
709 {
710     HANDLE hFile;
711     char temp_path[MAX_PATH], dirname[MAX_PATH];
712     char filename[MAX_PATH];
713     static const char prefix[] = "pfx";
714     char windowsdir[MAX_PATH];
715     char Volume_1[MAX_PATH];
716     unsigned char buffer[512];
717     char directory[] = "removeme";
718     static const char nt_drive[] = "\\\\?\\A:";
719     DWORD i, ret, len;
720     struct test_list p[] = {
721     {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
722     {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
723     {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
724     {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
725     {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
726     {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
727     {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
728     {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
729     {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
730     {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
731     {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
732     {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
733     {NULL, 0, -1, 0, FALSE}
734     };
735     BY_HANDLE_FILE_INFORMATION  Finfo;
736
737     ret = GetTempPathA(MAX_PATH, temp_path);
738     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
739     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
740
741     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
742     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
743
744     SetLastError(0xdeadbeef);
745     hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
746                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
747     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
748         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
749
750     SetLastError(0xdeadbeef);
751     hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
752                         CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
753     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
754        "hFile %p, last error %u\n", hFile, GetLastError());
755
756     CloseHandle(hFile);
757
758     SetLastError(0xdeadbeef);
759     hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
760                         OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
761     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
762        "hFile %p, last error %u\n", hFile, GetLastError());
763
764     CloseHandle(hFile);
765
766     ret = DeleteFileA(filename);
767     ok(ret, "DeleteFileA: error %d\n", GetLastError());
768
769     SetLastError(0xdeadbeef);
770     hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
771                         OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
772     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
773        "hFile %p, last error %u\n", hFile, GetLastError());
774
775     CloseHandle(hFile);
776
777     ret = DeleteFileA(filename);
778     ok(ret, "DeleteFileA: error %d\n", GetLastError());
779
780     /* get windows drive letter */
781     ret = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
782     ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
783     ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
784
785     /* test error return codes from CreateFile for some cases */
786     ret = GetTempPathA(MAX_PATH, temp_path);
787     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
788     strcpy(dirname, temp_path);
789     strcat(dirname, directory);
790     ret = CreateDirectory(dirname, NULL);
791     ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
792     /* set current drive & directory to known location */
793     SetCurrentDirectoryA( temp_path );
794     i = 0;
795     while (p[i].file)
796     {
797         filename[0] = 0;
798         /* update the drive id in the table entry with the current one */
799         if (p[i].file[1] == ':')
800         {
801             strcpy(filename, p[i].file);
802             filename[0] = windowsdir[0];
803         }
804         else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
805         {
806             strcpy(filename, p[i].file);
807             filename[4] = windowsdir[0];
808         }
809         else
810         {
811             /* prefix the table entry with the current temp directory */
812             strcpy(filename, temp_path);
813             strcat(filename, p[i].file);
814         }
815         hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
816                         FILE_SHARE_READ | FILE_SHARE_WRITE,
817                         NULL, OPEN_EXISTING,
818                         p[i].options, NULL );
819         /* if we get ACCESS_DENIED when we do not expect it, assume
820          * no access to the volume
821          */
822         if (hFile == INVALID_HANDLE_VALUE &&
823             GetLastError() == ERROR_ACCESS_DENIED &&
824             p[i].err != ERROR_ACCESS_DENIED)
825         {
826             if (p[i].todo_flag)
827                 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
828             else
829                 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
830         }
831         /* otherwise validate results with expectations */
832         else if (p[i].todo_flag)
833             todo_wine ok(
834                 (hFile == INVALID_HANDLE_VALUE &&
835                   (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
836                 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
837                 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
838                 filename, hFile, GetLastError(), p[i].err);
839         else
840             ok(
841                 (hFile == INVALID_HANDLE_VALUE &&
842                  (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
843                 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
844                 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
845                 filename, hFile, GetLastError(), p[i].err);
846         if (hFile != INVALID_HANDLE_VALUE)
847             CloseHandle( hFile );
848         i++;
849     }
850     ret = RemoveDirectoryA(dirname);
851     ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
852
853
854     /* test opening directory as a directory */
855     hFile = CreateFileA( temp_path, GENERIC_READ,
856                         FILE_SHARE_READ,
857                         NULL,
858                         OPEN_EXISTING,
859                         FILE_FLAG_BACKUP_SEMANTICS, NULL );
860     if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
861     {
862         ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
863             "CreateFileA did not work, last error %u on volume <%s>\n",
864              GetLastError(), temp_path );
865
866         if (hFile != INVALID_HANDLE_VALUE)
867         {
868             ret = GetFileInformationByHandle( hFile, &Finfo );
869             if (ret)
870             {
871                 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
872                     "CreateFileA probably did not open temp directory %s correctly\n   file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
873                 temp_path, Finfo.dwFileAttributes);
874             }
875             CloseHandle( hFile );
876         }
877     }
878     else
879         skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
880
881
882     /* ***  Test opening volumes/devices using drive letter  ***         */
883
884     /* test using drive letter in non-rewrite format without trailing \  */
885     /* this should work                                                  */
886     strcpy(filename, nt_drive);
887     filename[4] = windowsdir[0];
888     hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
889                         FILE_SHARE_READ | FILE_SHARE_WRITE,
890                         NULL, OPEN_EXISTING,
891                         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
892     if (hFile != INVALID_HANDLE_VALUE ||
893         (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
894     {
895         /* if we have adm rights to volume, then try rest of tests */
896         ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
897             filename, GetLastError());
898         if (hFile != INVALID_HANDLE_VALUE)
899         {
900             /* if we opened the volume/device, try to read it. Since it  */
901             /* opened, we should be able to read it.  We don't care about*/
902             /* what the data is at this time.                            */
903             len = 512;
904             ret = ReadFile( hFile, buffer, len, &len, NULL );
905             todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
906                 GetLastError(), ret, filename);
907             if (ret)
908             {
909                 trace("buffer is\n");
910                 dumpmem(buffer, 64);
911             }
912             CloseHandle( hFile );
913         }
914
915         /* test using drive letter with trailing \ and in non-rewrite   */
916         /* this should not work                                         */
917         strcpy(filename, nt_drive);
918         filename[4] = windowsdir[0];
919         strcat( filename, "\\" );
920         hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
921                         FILE_SHARE_READ | FILE_SHARE_WRITE,
922                         NULL, OPEN_EXISTING,
923                         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
924         todo_wine
925         ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
926             "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
927             filename, GetLastError());
928         if (hFile != INVALID_HANDLE_VALUE)
929             CloseHandle( hFile );
930
931         /* test using temp path with trailing \ and in non-rewrite as dir */
932         /* this should work                                               */
933         strcpy(filename, nt_drive);
934         filename[4] = 0;
935         strcat( filename, temp_path );
936         hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
937                         FILE_SHARE_READ | FILE_SHARE_WRITE,
938                         NULL, OPEN_EXISTING,
939                         FILE_FLAG_BACKUP_SEMANTICS, NULL );
940         ok(hFile != INVALID_HANDLE_VALUE,
941             "CreateFileA should have worked on %s, but got %u\n",
942             filename, GetLastError());
943         if (hFile != INVALID_HANDLE_VALUE)
944             CloseHandle( hFile );
945
946         /* test using drive letter without trailing \ and in device ns  */
947         /* this should work                                             */
948         strcpy(filename, nt_drive);
949         filename[4] = windowsdir[0];
950         filename[2] = '.';
951         hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
952                         FILE_SHARE_READ | FILE_SHARE_WRITE,
953                         NULL, OPEN_EXISTING,
954                         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
955         ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
956             filename, GetLastError());
957         if (hFile != INVALID_HANDLE_VALUE)
958             CloseHandle( hFile );
959     }
960     /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
961     else if (GetLastError() == ERROR_BAD_NETPATH)
962         skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
963     else
964         skip("Do not have authority to access volumes. Tests skipped\n");
965
966
967     /* ***  Test opening volumes/devices using GUID  ***           */
968
969     if (pGetVolumeNameForVolumeMountPointA)
970     {
971         strcpy(filename, "c:\\");
972         filename[0] = windowsdir[0];
973         ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
974         ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
975         if (ret)
976         {
977             ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
978
979             /* test the result of opening a unique volume name (GUID)
980              *  with the trailing \
981              *  this should error out
982              */
983             strcpy(filename, Volume_1);
984             hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
985                         FILE_SHARE_READ | FILE_SHARE_WRITE,
986                         NULL, OPEN_EXISTING,
987                         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
988             todo_wine
989             ok(hFile == INVALID_HANDLE_VALUE,
990                 "CreateFileA should not have opened %s, hFile %p\n",
991                 filename, hFile);
992             todo_wine
993             ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
994                 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
995                 filename, GetLastError());
996             if (hFile != INVALID_HANDLE_VALUE)
997                 CloseHandle( hFile );
998
999             /* test the result of opening a unique volume name (GUID)
1000              * with the temp path string as dir
1001              * this should work
1002              */
1003             strcpy(filename, Volume_1);
1004             strcat(filename, temp_path+3);
1005             hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1006                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1007                         NULL, OPEN_EXISTING,
1008                         FILE_FLAG_BACKUP_SEMANTICS, NULL );
1009             todo_wine
1010             ok(hFile != INVALID_HANDLE_VALUE,
1011                 "CreateFileA should have opened %s, but got %u\n",
1012                 filename, GetLastError());
1013             if (hFile != INVALID_HANDLE_VALUE)
1014                 CloseHandle( hFile );
1015
1016             /* test the result of opening a unique volume name (GUID)
1017              * without the trailing \ and in device namespace
1018              * this should work
1019              */
1020             strcpy(filename, Volume_1);
1021             filename[2] = '.';
1022             filename[48] = 0;
1023             hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1024                         FILE_SHARE_READ | FILE_SHARE_WRITE,
1025                         NULL, OPEN_EXISTING,
1026                         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1027             if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1028             {
1029                 /* if we have adm rights to volume, then try rest of tests */
1030                 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1031                     filename, GetLastError());
1032                 if (hFile != INVALID_HANDLE_VALUE)
1033                 {
1034                     /* if we opened the volume/device, try to read it. Since it  */
1035                     /* opened, we should be able to read it.  We don't care about*/
1036                     /* what the data is at this time.                            */
1037                     len = 512;
1038                     ret = ReadFile( hFile, buffer, len, &len, NULL );
1039                     todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1040                         GetLastError(), ret, filename);
1041                     if (ret)
1042                     {
1043                         trace("buffer is\n");
1044                         dumpmem(buffer, 64);
1045                     }
1046                     CloseHandle( hFile );
1047                 }
1048             }
1049             else
1050                 skip("Do not have authority to access volumes. Tests skipped\n");
1051         }
1052         else
1053             win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1054     }
1055     else
1056         win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1057 }
1058
1059 static void test_CreateFileW(void)
1060 {
1061     HANDLE hFile;
1062     WCHAR temp_path[MAX_PATH];
1063     WCHAR filename[MAX_PATH];
1064     static const WCHAR emptyW[]={'\0'};
1065     static const WCHAR prefix[] = {'p','f','x',0};
1066     static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1067     DWORD ret;
1068
1069     ret = GetTempPathW(MAX_PATH, temp_path);
1070     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1071     {
1072         win_skip("GetTempPathW is not available\n");
1073         return;
1074     }
1075     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1076     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1077
1078     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1079     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1080
1081     SetLastError(0xdeadbeef);
1082     hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1083                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1084     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1085         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1086
1087     SetLastError(0xdeadbeef);
1088     hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1089                         CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1090     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1091        "hFile %p, last error %u\n", hFile, GetLastError());
1092
1093     CloseHandle(hFile);
1094
1095     SetLastError(0xdeadbeef);
1096     hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1097                         OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1098     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1099        "hFile %p, last error %u\n", hFile, GetLastError());
1100
1101     CloseHandle(hFile);
1102
1103     ret = DeleteFileW(filename);
1104     ok(ret, "DeleteFileW: error %d\n", GetLastError());
1105
1106     SetLastError(0xdeadbeef);
1107     hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1108                         OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1109     ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1110        "hFile %p, last error %u\n", hFile, GetLastError());
1111
1112     CloseHandle(hFile);
1113
1114     ret = DeleteFileW(filename);
1115     ok(ret, "DeleteFileW: error %d\n", GetLastError());
1116
1117     if (0)
1118     {
1119         /* this crashes on NT4.0 */
1120         hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1121                             CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1122         ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1123            "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1124     }
1125
1126     hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1127                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1128     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1129        "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1130
1131     /* test the result of opening a nonexistent driver name */
1132     hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1133                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1134     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1135        "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1136
1137     ret = CreateDirectoryW(filename, NULL);
1138     ok(ret == TRUE, "couldn't create temporary directory\n");
1139     hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1140                         OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1141     ok(hFile != INVALID_HANDLE_VALUE,
1142        "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1143     CloseHandle(hFile);
1144     ret = RemoveDirectoryW(filename);
1145     ok(ret, "DeleteFileW: error %d\n", GetLastError());
1146 }
1147
1148 static void test_GetTempFileNameA(void)
1149 {
1150     UINT result;
1151     char out[MAX_PATH];
1152     char expected[MAX_PATH + 10];
1153     char windowsdir[MAX_PATH + 10];
1154     char windowsdrive[3];
1155
1156     result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1157     ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1158     ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1159
1160     /* If the Windows directory is the root directory, it ends in backslash, not else. */
1161     if (strlen(windowsdir) != 3) /* As in  "C:\"  or  "F:\"  */
1162     {
1163         strcat(windowsdir, "\\");
1164     }
1165
1166     windowsdrive[0] = windowsdir[0];
1167     windowsdrive[1] = windowsdir[1];
1168     windowsdrive[2] = '\0';
1169
1170     result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1171     ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1172     ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1173        "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1174        windowsdrive[0], out);
1175
1176     result = GetTempFileNameA(windowsdir, "abc", 2, out);
1177     ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1178     expected[0] = '\0';
1179     strcat(expected, windowsdir);
1180     strcat(expected, "abc2.tmp");
1181     ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1182        out, expected);
1183 }
1184
1185 static void test_DeleteFileA( void )
1186 {
1187     BOOL ret;
1188
1189     ret = DeleteFileA(NULL);
1190     ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1191                 GetLastError() == ERROR_PATH_NOT_FOUND),
1192        "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1193
1194     ret = DeleteFileA("");
1195     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1196                 GetLastError() == ERROR_BAD_PATHNAME),
1197        "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1198
1199     ret = DeleteFileA("nul");
1200     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1201                 GetLastError() == ERROR_INVALID_PARAMETER ||
1202                 GetLastError() == ERROR_ACCESS_DENIED ||
1203                 GetLastError() == ERROR_INVALID_FUNCTION),
1204        "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1205 }
1206
1207 static void test_DeleteFileW( void )
1208 {
1209     BOOL ret;
1210     WCHAR pathW[MAX_PATH];
1211     WCHAR pathsubW[MAX_PATH];
1212     static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1213     static const WCHAR subdirW[] = {'\\','s','u','b',0};
1214     static const WCHAR emptyW[]={'\0'};
1215
1216     ret = DeleteFileW(NULL);
1217     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1218     {
1219         win_skip("DeleteFileW is not available\n");
1220         return;
1221     }
1222     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1223        "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1224
1225     ret = DeleteFileW(emptyW);
1226     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1227        "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1228
1229     /* test DeleteFile on empty directory */
1230     ret = GetTempPathW(MAX_PATH, pathW);
1231     if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1232     {
1233         ok(0, "MAX_PATH exceeded in constructing paths\n");
1234         return;
1235     }
1236     lstrcatW(pathW, dirW);
1237     lstrcpyW(pathsubW, pathW);
1238     lstrcatW(pathsubW, subdirW);
1239     ret = CreateDirectoryW(pathW, NULL);
1240     ok(ret == TRUE, "couldn't create directory deletefile\n");
1241     ret = DeleteFileW(pathW);
1242     ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1243     ret = RemoveDirectoryW(pathW);
1244     ok(ret == TRUE, "expected to remove directory deletefile\n");
1245
1246     /* test DeleteFile on non-empty directory */
1247     ret = CreateDirectoryW(pathW, NULL);
1248     ok(ret == TRUE, "couldn't create directory deletefile\n");
1249     ret = CreateDirectoryW(pathsubW, NULL);
1250     ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1251     ret = DeleteFileW(pathW);
1252     ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1253     ret = RemoveDirectoryW(pathsubW);
1254     ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1255     ret = RemoveDirectoryW(pathW);
1256     ok(ret == TRUE, "expected to remove directory deletefile\n");
1257 }
1258
1259 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1260
1261 static void test_MoveFileA(void)
1262 {
1263     char tempdir[MAX_PATH];
1264     char source[MAX_PATH], dest[MAX_PATH];
1265     static const char prefix[] = "pfx";
1266     DWORD ret;
1267
1268     ret = GetTempPathA(MAX_PATH, tempdir);
1269     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1270     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1271
1272     ret = GetTempFileNameA(tempdir, prefix, 0, source);
1273     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1274
1275     ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1276     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1277
1278     ret = MoveFileA(source, dest);
1279     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1280        "MoveFileA: unexpected error %d\n", GetLastError());
1281
1282     ret = DeleteFileA(dest);
1283     ok(ret, "DeleteFileA: error %d\n", GetLastError());
1284
1285     ret = MoveFileA(source, dest);
1286     ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1287
1288     lstrcatA(tempdir, "Remove Me");
1289     ret = CreateDirectoryA(tempdir, NULL);
1290     ok(ret == TRUE, "CreateDirectoryA failed\n");
1291
1292     lstrcpyA(source, dest);
1293     lstrcpyA(dest, tempdir);
1294     lstrcatA(dest, "\\wild?.*");
1295     /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1296     ret = MoveFileA(source, dest);
1297     ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1298     ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1299        GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1300        "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1301     if (ret || (GetLastError() != ERROR_INVALID_NAME))
1302     {
1303         WIN32_FIND_DATAA fd;
1304         char temppath[MAX_PATH];
1305         HANDLE hFind;
1306
1307         lstrcpyA(temppath, tempdir);
1308         lstrcatA(temppath, "\\*.*");
1309         hFind = FindFirstFileA(temppath, &fd);
1310         if (INVALID_HANDLE_VALUE != hFind)
1311         {
1312           LPSTR lpName;
1313           do
1314           {
1315             lpName = fd.cAlternateFileName;
1316             if (!lpName[0])
1317               lpName = fd.cFileName;
1318             ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1319           }
1320           while (FindNextFileA(hFind, &fd));
1321           FindClose(hFind);
1322         }
1323     }
1324     ret = DeleteFileA(source);
1325     ok(ret, "DeleteFileA: error %d\n", GetLastError());
1326     ret = DeleteFileA(dest);
1327     ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1328     ret = RemoveDirectoryA(tempdir);
1329     ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1330 }
1331
1332 static void test_MoveFileW(void)
1333 {
1334     WCHAR temp_path[MAX_PATH];
1335     WCHAR source[MAX_PATH], dest[MAX_PATH];
1336     static const WCHAR prefix[] = {'p','f','x',0};
1337     DWORD ret;
1338
1339     ret = GetTempPathW(MAX_PATH, temp_path);
1340     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1341     {
1342         win_skip("GetTempPathW is not available\n");
1343         return;
1344     }
1345     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1346     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1347
1348     ret = GetTempFileNameW(temp_path, prefix, 0, source);
1349     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1350
1351     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1352     ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1353
1354     ret = MoveFileW(source, dest);
1355     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1356        "CopyFileW: unexpected error %d\n", GetLastError());
1357
1358     ret = DeleteFileW(source);
1359     ok(ret, "DeleteFileW: error %d\n", GetLastError());
1360     ret = DeleteFileW(dest);
1361     ok(ret, "DeleteFileW: error %d\n", GetLastError());
1362 }
1363
1364 #define PATTERN_OFFSET 0x10
1365
1366 static void test_offset_in_overlapped_structure(void)
1367 {
1368     HANDLE hFile;
1369     OVERLAPPED ov;
1370     DWORD done, offset;
1371     BOOL rc;
1372     BYTE buf[256], pattern[] = "TeSt";
1373     UINT i;
1374     char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1375     BOOL ret;
1376
1377     ret =GetTempPathA(MAX_PATH, temp_path);
1378     ok( ret, "GetTempPathA error %d\n", GetLastError());
1379     ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1380     ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1381
1382     /*** Write File *****************************************************/
1383
1384     hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1385     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1386
1387     for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1388     ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1389     ok( ret, "WriteFile error %d\n", GetLastError());
1390     ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1391
1392     memset(&ov, 0, sizeof(ov));
1393     S(U(ov)).Offset = PATTERN_OFFSET;
1394     S(U(ov)).OffsetHigh = 0;
1395     rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1396     /* Win 9x does not support the overlapped I/O on files */
1397     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1398         ok(rc, "WriteFile error %d\n", GetLastError());
1399         ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1400         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1401         ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1402
1403         S(U(ov)).Offset = sizeof(buf) * 2;
1404         S(U(ov)).OffsetHigh = 0;
1405         ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1406         ok( ret, "WriteFile error %d\n", GetLastError());
1407         ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1408         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1409         ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1410     }
1411
1412     CloseHandle(hFile);
1413
1414     /*** Read File *****************************************************/
1415
1416     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1417     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1418
1419     memset(buf, 0, sizeof(buf));
1420     memset(&ov, 0, sizeof(ov));
1421     S(U(ov)).Offset = PATTERN_OFFSET;
1422     S(U(ov)).OffsetHigh = 0;
1423     rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1424     /* Win 9x does not support the overlapped I/O on files */
1425     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1426         ok(rc, "ReadFile error %d\n", GetLastError());
1427         ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1428         offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1429         ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1430         ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1431     }
1432
1433     CloseHandle(hFile);
1434
1435     ret = DeleteFileA(temp_fname);
1436     ok( ret, "DeleteFileA error %d\n", GetLastError());
1437 }
1438
1439 static void test_LockFile(void)
1440 {
1441     HANDLE handle;
1442     DWORD written;
1443     OVERLAPPED overlapped;
1444     int limited_LockFile;
1445     int limited_UnLockFile;
1446     BOOL ret;
1447
1448     handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1449                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1450                           CREATE_ALWAYS, 0, 0 );
1451     if (handle == INVALID_HANDLE_VALUE)
1452     {
1453         ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1454         return;
1455     }
1456     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1457
1458     ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1459     ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1460
1461     limited_UnLockFile = 0;
1462     if (UnlockFile( handle, 0, 0, 0, 0 ))
1463     {
1464         limited_UnLockFile = 1;
1465     }
1466
1467     ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1468     /* overlapping locks must fail */
1469     ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1470     ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1471     /* non-overlapping locks must succeed */
1472     ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1473
1474     ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1475     ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1476     ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1477     ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1478
1479     S(U(overlapped)).Offset = 100;
1480     S(U(overlapped)).OffsetHigh = 0;
1481     overlapped.hEvent = 0;
1482
1483     /* Test for broken LockFileEx a la Windows 95 OSR2. */
1484     if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1485     {
1486         /* LockFileEx is probably OK, test it more. */
1487         ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1488             "LockFileEx 100,100 failed\n" );
1489     }
1490
1491     /* overlapping shared locks are OK */
1492     S(U(overlapped)).Offset = 150;
1493     limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1494
1495     /* but exclusive is not */
1496     ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1497                      0, 50, 0, &overlapped ),
1498         "LockFileEx exclusive 150,50 succeeded\n" );
1499     if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1500     { /* UnLockFile is capable. */
1501         S(U(overlapped)).Offset = 100;
1502         ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1503             "UnlockFileEx 150,100 again succeeded\n" );
1504     }
1505
1506     ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
1507     if (ret)
1508     {
1509         ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1510         ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1511         ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1512     }
1513     else  /* win9x */
1514         ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
1515
1516     /* wrap-around lock should not do anything */
1517     /* (but still succeeds on NT4 so we don't check result) */
1518     LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1519
1520     limited_LockFile = 0;
1521     if (!LockFile( handle, ~0, ~0, 1, 0 ))
1522     {
1523         limited_LockFile = 1;
1524     }
1525
1526     limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1527
1528     /* zero-byte lock */
1529     ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1530     limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1531     ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1532     limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1533
1534     ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1535     !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1536
1537     ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1538
1539     CloseHandle( handle );
1540     DeleteFileA( filename );
1541 }
1542
1543 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2, BOOL is_win9x )
1544 {
1545     if (!is_win9x)
1546     {
1547         if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1548         if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1549     }
1550     else
1551     {
1552         access1 &= ~DELETE;
1553         if (!access1) access1 = GENERIC_READ;
1554
1555         access2 &= ~DELETE;
1556         if (!access2) access2 = GENERIC_READ;
1557     }
1558
1559     if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1560     if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1561     if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1562     if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1563     if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1564     if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1565     return 1;
1566 }
1567
1568 static void test_file_sharing(void)
1569 {
1570     static const DWORD access_modes[] =
1571         { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1572           DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE };
1573     static const DWORD sharing_modes[] =
1574         { 0, FILE_SHARE_READ,
1575           FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1576           FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1577           FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1578     int a1, s1, a2, s2;
1579     int ret;
1580     HANDLE h, h2;
1581     BOOL is_win9x = FALSE;
1582
1583     /* make sure the file exists */
1584     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1585     if (h == INVALID_HANDLE_VALUE)
1586     {
1587         ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1588         return;
1589     }
1590     is_win9x = GetFileAttributesW(filenameW) == INVALID_FILE_ATTRIBUTES;
1591     CloseHandle( h );
1592
1593     for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1594     {
1595         for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1596         {
1597             /* Win9x doesn't support FILE_SHARE_DELETE */
1598             if (is_win9x && (sharing_modes[s1] & FILE_SHARE_DELETE))
1599                 continue;
1600
1601             SetLastError(0xdeadbeef);
1602             h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1603                              NULL, OPEN_EXISTING, 0, 0 );
1604             if (h == INVALID_HANDLE_VALUE)
1605             {
1606                 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1607                 return;
1608             }
1609             for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1610             {
1611                 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1612                 {
1613                     /* Win9x doesn't support FILE_SHARE_DELETE */
1614                     if (is_win9x && (sharing_modes[s2] & FILE_SHARE_DELETE))
1615                         continue;
1616
1617                     SetLastError(0xdeadbeef);
1618                     h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1619                                       NULL, OPEN_EXISTING, 0, 0 );
1620
1621                     if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1622                                                access_modes[a2], sharing_modes[s2], is_win9x ))
1623                     {
1624                         ret = GetLastError();
1625
1626                         ok( h2 != INVALID_HANDLE_VALUE,
1627                             "open failed for modes %x/%x/%x/%x\n",
1628                             access_modes[a1], sharing_modes[s1],
1629                             access_modes[a2], sharing_modes[s2] );
1630                         ok( ret == 0xdeadbeef /* Win9x */ ||
1631                             ret == 0, /* XP */
1632                              "wrong error code %d\n", ret );
1633
1634                         CloseHandle( h2 );
1635                     }
1636                     else
1637                     {
1638                         ret = GetLastError();
1639
1640                         ok( h2 == INVALID_HANDLE_VALUE,
1641                             "open succeeded for modes %x/%x/%x/%x\n",
1642                             access_modes[a1], sharing_modes[s1],
1643                             access_modes[a2], sharing_modes[s2] );
1644                          ok( ret == ERROR_SHARING_VIOLATION,
1645                              "wrong error code %d\n", ret );
1646                     }
1647                 }
1648             }
1649             CloseHandle( h );
1650         }
1651     }
1652
1653     SetLastError(0xdeadbeef);
1654     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1655     ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1656
1657     SetLastError(0xdeadbeef);
1658     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1659     ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1660     ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1661
1662     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1663     ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1664
1665     CloseHandle(h);
1666     CloseHandle(h2);
1667
1668     DeleteFileA( filename );
1669 }
1670
1671 static char get_windows_drive(void)
1672 {
1673     char windowsdir[MAX_PATH];
1674     GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1675     return windowsdir[0];
1676 }
1677
1678 static void test_FindFirstFileA(void)
1679 {
1680     HANDLE handle;
1681     WIN32_FIND_DATAA data;
1682     int err;
1683     char buffer[5] = "C:\\";
1684     char buffer2[100];
1685     char nonexistent[MAX_PATH];
1686
1687     /* try FindFirstFileA on "C:\" */
1688     buffer[0] = get_windows_drive();
1689     
1690     SetLastError( 0xdeadbeaf );
1691     handle = FindFirstFileA(buffer, &data);
1692     err = GetLastError();
1693     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
1694     ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1695
1696     /* try FindFirstFileA on "C:\*" */
1697     strcpy(buffer2, buffer);
1698     strcat(buffer2, "*");
1699     handle = FindFirstFileA(buffer2, &data);
1700     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1701     ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1702          "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
1703     if (FindNextFileA( handle, &data ))
1704         ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1705              "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
1706     ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1707
1708     /* try FindFirstFileA on windows dir */
1709     GetWindowsDirectory( buffer2, sizeof(buffer2) );
1710     strcat(buffer2, "\\*");
1711     handle = FindFirstFileA(buffer2, &data);
1712     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
1713     ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
1714     ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
1715     ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
1716     while (FindNextFileA( handle, &data ))
1717         ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
1718              "FindNextFile shouldn't return '%s'\n", data.cFileName );
1719     ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
1720
1721     /* try FindFirstFileA on "C:\foo\" */
1722     SetLastError( 0xdeadbeaf );
1723     if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
1724     {
1725         char tmp[MAX_PATH];
1726         GetTempPathA( sizeof(tmp), tmp );
1727         GetTempFileNameA( tmp, "foo", 0, nonexistent );
1728     }
1729     DeleteFileA( nonexistent );
1730     strcpy(buffer2, nonexistent);
1731     strcat(buffer2, "\\");
1732     handle = FindFirstFileA(buffer2, &data);
1733     err = GetLastError();
1734     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1735     todo_wine {
1736         ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1737     }
1738
1739     /* try FindFirstFileA on "C:\foo\bar.txt" */
1740     SetLastError( 0xdeadbeaf );
1741     strcpy(buffer2, nonexistent);
1742     strcat(buffer2, "\\bar.txt");
1743     handle = FindFirstFileA(buffer2, &data);
1744     err = GetLastError();
1745     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1746     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1747
1748     /* try FindFirstFileA on "C:\foo\*.*" */
1749     SetLastError( 0xdeadbeaf );
1750     strcpy(buffer2, nonexistent);
1751     strcat(buffer2, "\\*.*");
1752     handle = FindFirstFileA(buffer2, &data);
1753     err = GetLastError();
1754     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1755     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1756
1757     /* try FindFirstFileA on "foo\bar.txt" */
1758     SetLastError( 0xdeadbeaf );
1759     strcpy(buffer2, nonexistent + 3);
1760     strcat(buffer2, "\\bar.txt");
1761     handle = FindFirstFileA(buffer2, &data);
1762     err = GetLastError();
1763     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1764     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1765
1766     /* try FindFirstFileA on "c:\nul" */
1767     SetLastError( 0xdeadbeaf );
1768     strcpy(buffer2, buffer);
1769     strcat(buffer2, "nul");
1770     handle = FindFirstFileA(buffer2, &data);
1771     err = GetLastError();
1772     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1773     ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
1774     ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1775         FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1776         "wrong attributes %x\n", data.dwFileAttributes );
1777     if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1778     {
1779         ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1780         ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1781     }
1782     SetLastError( 0xdeadbeaf );
1783     ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1784     ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1785     ok( FindClose( handle ), "failed to close handle\n" );
1786
1787     /* try FindFirstFileA on "lpt1" */
1788     SetLastError( 0xdeadbeaf );
1789     strcpy(buffer2, "lpt1");
1790     handle = FindFirstFileA(buffer2, &data);
1791     err = GetLastError();
1792     ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed\n", buffer2 );
1793     ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
1794     ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
1795         FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
1796         "wrong attributes %x\n", data.dwFileAttributes );
1797     if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
1798     {
1799         ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
1800         ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
1801     }
1802     SetLastError( 0xdeadbeaf );
1803     ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
1804     ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
1805     ok( FindClose( handle ), "failed to close handle\n" );
1806
1807     /* try FindFirstFileA on "c:\nul\*" */
1808     SetLastError( 0xdeadbeaf );
1809     strcpy(buffer2, buffer);
1810     strcat(buffer2, "nul\\*");
1811     handle = FindFirstFileA(buffer2, &data);
1812     err = GetLastError();
1813     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1814     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1815
1816     /* try FindFirstFileA on "c:\nul*" */
1817     SetLastError( 0xdeadbeaf );
1818     strcpy(buffer2, buffer);
1819     strcat(buffer2, "nul*");
1820     handle = FindFirstFileA(buffer2, &data);
1821     err = GetLastError();
1822     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1823     ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
1824
1825     /* try FindFirstFileA on "c:\foo\bar\nul" */
1826     SetLastError( 0xdeadbeaf );
1827     strcpy(buffer2, buffer);
1828     strcat(buffer2, "foo\\bar\\nul");
1829     handle = FindFirstFileA(buffer2, &data);
1830     err = GetLastError();
1831     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1832     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1833
1834     /* try FindFirstFileA on "c:\foo\nul\bar" */
1835     SetLastError( 0xdeadbeaf );
1836     strcpy(buffer2, buffer);
1837     strcat(buffer2, "foo\\nul\\bar");
1838     handle = FindFirstFileA(buffer2, &data);
1839     err = GetLastError();
1840     ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
1841     ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
1842 }
1843
1844 static void test_FindNextFileA(void)
1845 {
1846     HANDLE handle;
1847     WIN32_FIND_DATAA search_results;
1848     int err;
1849     char buffer[5] = "C:\\*";
1850
1851     buffer[0] = get_windows_drive();
1852     handle = FindFirstFileA(buffer,&search_results);
1853     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1854     while (FindNextFile(handle, &search_results))
1855     {
1856         /* get to the end of the files */
1857     }
1858     ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1859     err = GetLastError();
1860     ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1861 }
1862
1863 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops)
1864 {
1865     WIN32_FIND_DATAA search_results;
1866     HANDLE handle;
1867     BOOL ret;
1868
1869     if (!pFindFirstFileExA)
1870     {
1871         win_skip("FindFirstFileExA() is missing\n");
1872         return;
1873     }
1874
1875     CreateDirectoryA("test-dir", NULL);
1876     _lclose(_lcreat("test-dir\\file1", 0));
1877     _lclose(_lcreat("test-dir\\file2", 0));
1878     CreateDirectoryA("test-dir\\dir1", NULL);
1879     SetLastError(0xdeadbeef);
1880     handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, search_ops, NULL, 0);
1881     if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1882     {
1883         win_skip("FindFirstFileExA is not implemented\n");
1884         goto cleanup;
1885     }
1886     ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
1887     ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
1888
1889 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
1890
1891     ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
1892     ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
1893
1894     ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
1895     ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
1896
1897     SetLastError(0xdeadbeef);
1898     ret = FindNextFile(handle, &search_results);
1899     if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
1900     {
1901         skip("File system supports directory filtering\n");
1902         /* Results from the previous call are not cleared */
1903         ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
1904         FindClose( handle );
1905         goto cleanup;
1906     }
1907
1908     ok(ret, "Fetching fourth file failed\n");
1909     ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
1910
1911     ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
1912     ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
1913
1914 #undef CHECK_NAME
1915
1916     ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
1917
1918     FindClose( handle );
1919
1920 cleanup:
1921     DeleteFileA("test-dir\\file1");
1922     DeleteFileA("test-dir\\file2");
1923     RemoveDirectoryA("test-dir\\dir1");
1924     RemoveDirectoryA("test-dir");
1925 }
1926
1927 static int test_Mapfile_createtemp(HANDLE *handle)
1928 {
1929     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1930     DeleteFile(filename);
1931     *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1932                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1933     if (*handle != INVALID_HANDLE_VALUE) {
1934
1935         return 1;
1936     }
1937
1938     return 0;
1939 }
1940
1941 static void test_MapFile(void)
1942 {
1943     HANDLE handle;
1944     HANDLE hmap;
1945
1946     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1947
1948     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1949     ok( hmap != NULL, "mapping should work, I named it!\n" );
1950
1951     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1952
1953     /* We have to close file before we try new stuff with mapping again.
1954        Else we would always succeed on XP or block descriptors on 95. */
1955     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1956     ok( hmap != NULL, "We should still be able to map!\n" );
1957     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1958     ok( CloseHandle( handle ), "can't close file handle\n");
1959     handle = NULL;
1960
1961     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1962
1963     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1964     ok( hmap == NULL, "mapped zero size file\n");
1965     ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1966
1967     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
1968     ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1969     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1970     if ( hmap )
1971         CloseHandle( hmap );
1972
1973     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
1974     ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
1975     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1976     if ( hmap )
1977         CloseHandle( hmap );
1978
1979     /* On XP you can now map again, on Win 95 you cannot. */
1980
1981     ok( CloseHandle( handle ), "can't close file handle\n");
1982     ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1983 }
1984
1985 static void test_GetFileType(void)
1986 {
1987     DWORD type;
1988     HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1989     ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1990     type = GetFileType(h);
1991     ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
1992     CloseHandle( h );
1993     h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1994     ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1995     type = GetFileType(h);
1996     ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
1997     CloseHandle( h );
1998     DeleteFileA( filename );
1999 }
2000
2001 static int completion_count;
2002
2003 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2004 {
2005 /*      printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2006         ReleaseSemaphore(ovl->hEvent, 1, NULL);
2007         completion_count++;
2008 }
2009
2010 static void test_async_file_errors(void)
2011 {
2012     char szFile[MAX_PATH];
2013     HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2014     HANDLE hFile;
2015     LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2016     OVERLAPPED ovl;
2017     S(U(ovl)).Offset = 0;
2018     S(U(ovl)).OffsetHigh = 0;
2019     ovl.hEvent = hSem;
2020     completion_count = 0;
2021     szFile[0] = '\0';
2022     GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2023     strcat(szFile, "\\win.ini");
2024     hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2025                         NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2026     if (hFile == INVALID_HANDLE_VALUE)  /* win9x doesn't like FILE_SHARE_DELETE */
2027         hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2028                             NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2029     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2030     while (TRUE)
2031     {
2032         BOOL res;
2033         DWORD count;
2034         while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2035             ;
2036         res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2037         /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2038         if (!res)
2039             break;
2040         if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2041             break;
2042         S(U(ovl)).Offset += count;
2043         /* i/o completion routine only called if ReadFileEx returned success.
2044          * we only care about violations of this rule so undo what should have
2045          * been done */
2046         completion_count--;
2047     }
2048     ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2049     /*printf("Error = %ld\n", GetLastError());*/
2050     HeapFree(GetProcessHeap(), 0, lpBuffer);
2051 }
2052
2053 static void test_read_write(void)
2054 {
2055     DWORD bytes, ret, old_prot;
2056     HANDLE hFile;
2057     char temp_path[MAX_PATH];
2058     char filename[MAX_PATH];
2059     char *mem;
2060     static const char prefix[] = "pfx";
2061
2062     ret = GetTempPathA(MAX_PATH, temp_path);
2063     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2064     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2065
2066     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2067     ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2068
2069     hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2070                         CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2071     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2072
2073     SetLastError(12345678);
2074     bytes = 12345678;
2075     ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2076     ok(ret && GetLastError() == 12345678,
2077         "ret = %d, error %d\n", ret, GetLastError());
2078     ok(!bytes, "bytes = %d\n", bytes);
2079
2080     SetLastError(12345678);
2081     bytes = 12345678;
2082     ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2083     ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2084         (ret && GetLastError() == 12345678), /* Win9x */
2085         "ret = %d, error %d\n", ret, GetLastError());
2086     ok(!bytes || /* Win2k */
2087         bytes == 10, /* Win9x */
2088         "bytes = %d\n", bytes);
2089
2090     /* make sure the file contains data */
2091     WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2092     SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2093
2094     SetLastError(12345678);
2095     bytes = 12345678;
2096     ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2097     ok(ret && GetLastError() == 12345678,
2098         "ret = %d, error %d\n", ret, GetLastError());
2099     ok(!bytes, "bytes = %d\n", bytes);
2100
2101     SetLastError(12345678);
2102     bytes = 12345678;
2103     ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2104     ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
2105                 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
2106         "ret = %d, error %d\n", ret, GetLastError());
2107     ok(!bytes, "bytes = %d\n", bytes);
2108
2109     /* test passing protected memory as buffer */
2110
2111     mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
2112     ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
2113
2114     ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2115     ok( ret, "WriteFile failed error %u\n", GetLastError() );
2116     ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
2117
2118     ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
2119     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2120
2121     ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2122     ok( !ret, "WriteFile succeeded\n" );
2123     ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2124         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2125         "wrong error %u\n", GetLastError() );
2126     ok( bytes == 0, "wrote %x bytes\n", bytes );
2127
2128     ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
2129     ok( !ret, "WriteFile succeeded\n" );
2130     ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
2131         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2132         "wrong error %u\n", GetLastError() );
2133     ok( bytes == 0, "wrote %x bytes\n", bytes );
2134
2135     ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
2136     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2137
2138     ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2139     ok( !ret, "WriteFile succeeded\n" );
2140     ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2141         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2142         "wrong error %u\n", GetLastError() );
2143     ok( bytes == 0, "wrote %x bytes\n", bytes );
2144
2145     SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2146
2147     ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2148     ok( !ret, "ReadFile succeeded\n" );
2149     ok( GetLastError() == ERROR_NOACCESS ||
2150         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2151         "wrong error %u\n", GetLastError() );
2152     ok( bytes == 0, "read %x bytes\n", bytes );
2153
2154     ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
2155     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2156
2157     ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2158     ok( !ret, "ReadFile succeeded\n" );
2159     ok( GetLastError() == ERROR_NOACCESS ||
2160         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2161         "wrong error %u\n", GetLastError() );
2162     ok( bytes == 0, "read %x bytes\n", bytes );
2163
2164     ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
2165     ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2166
2167     ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2168     ok( !ret, "ReadFile succeeded\n" );
2169     ok( GetLastError() == ERROR_NOACCESS ||
2170         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2171         "wrong error %u\n", GetLastError() );
2172     ok( bytes == 0, "read %x bytes\n", bytes );
2173
2174     SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
2175     SetEndOfFile( hFile );
2176     SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2177
2178     ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2179     ok( !ret, "ReadFile succeeded\n" );
2180     ok( GetLastError() == ERROR_NOACCESS ||
2181         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2182         "wrong error %u\n", GetLastError() );
2183     ok( bytes == 0, "read %x bytes\n", bytes );
2184
2185     ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
2186     ok( ret, "ReadFile failed error %u\n", GetLastError() );
2187     ok( bytes == 0x1234, "read %x bytes\n", bytes );
2188
2189     ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
2190     ok( !ret, "ReadFile succeeded\n" );
2191     ok( GetLastError() == ERROR_NOACCESS ||
2192         GetLastError() == ERROR_INVALID_PARAMETER,  /* win9x */
2193         "wrong error %u\n", GetLastError() );
2194     ok( bytes == 0, "read %x bytes\n", bytes );
2195
2196     VirtualFree( mem, 0, MEM_FREE );
2197
2198     ret = CloseHandle(hFile);
2199     ok( ret, "CloseHandle: error %d\n", GetLastError());
2200     ret = DeleteFileA(filename);
2201     ok( ret, "DeleteFileA: error %d\n", GetLastError());
2202 }
2203
2204 static void test_OpenFile(void)
2205 {
2206     HFILE hFile;
2207     OFSTRUCT ofs;
2208     BOOL ret;
2209     DWORD retval;
2210     
2211     static const char file[] = "regedit.exe";
2212     static const char foo[] = ".\\foo-bar-foo.baz";
2213     static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
2214         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2215         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2216         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2217         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2218         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2219     char buff[MAX_PATH];
2220     char buff_long[4*MAX_PATH];
2221     char filled_0xA5[OFS_MAXPATHNAME];
2222     char *p;
2223     UINT length;
2224     
2225     /* Check for existing file */
2226     if (!pGetSystemWindowsDirectoryA)
2227         length = GetWindowsDirectoryA(buff, MAX_PATH);
2228     else
2229         length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
2230
2231     if (length + sizeof(file) < MAX_PATH)
2232     {
2233         p = buff + strlen(buff);
2234         if (p > buff && p[-1] != '\\') *p++ = '\\';
2235         strcpy( p, file );
2236         memset(&ofs, 0xA5, sizeof(ofs));
2237         SetLastError(0xfaceabee);
2238
2239         hFile = OpenFile(buff, &ofs, OF_EXIST);
2240         ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
2241         ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2242             "GetLastError() returns %d\n", GetLastError() );
2243         ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2244         ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2245         ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2246             "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2247             ofs.szPathName, buff );
2248     }
2249
2250     memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
2251     length = GetCurrentDirectoryA(MAX_PATH, buff);
2252
2253     /* Check for nonexistent file */
2254     if (length + sizeof(foo) < MAX_PATH)
2255     {
2256         p = buff + strlen(buff);
2257         if (p > buff && p[-1] != '\\') *p++ = '\\';
2258         strcpy( p, foo + 2 );
2259         memset(&ofs, 0xA5, sizeof(ofs));
2260         SetLastError(0xfaceabee);
2261
2262         hFile = OpenFile(foo, &ofs, OF_EXIST);
2263         ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2264         ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
2265         todo_wine
2266         ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2267         ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2268         ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2269             "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n", 
2270             ofs.szPathName, buff );
2271     }
2272
2273     length = GetCurrentDirectoryA(MAX_PATH, buff_long);
2274     length += lstrlenA(foo_too_long + 1);
2275
2276     /* Check for nonexistent file with too long filename */ 
2277     if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long)) 
2278     {
2279         lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
2280         memset(&ofs, 0xA5, sizeof(ofs));
2281         SetLastError(0xfaceabee);
2282
2283         hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
2284         ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2285         ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE, 
2286             "GetLastError() returns %d\n", GetLastError() );
2287         todo_wine
2288         ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2289         ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
2290             "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2291         ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0, 
2292             "OpenFile returned '%s', but was expected to return string filled with 0xA5\n", 
2293             ofs.szPathName );
2294     }
2295
2296     length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
2297
2298     if (length >= MAX_PATH) 
2299     {
2300         trace("Buffer too small, requested length = %d, but MAX_PATH = %d.  Skipping test.\n", length, MAX_PATH);
2301         return;
2302     }
2303     p = buff + strlen(buff);
2304     if (p > buff && p[-1] != '\\') *p++ = '\\';
2305     strcpy( p, filename );
2306
2307     memset(&ofs, 0xA5, sizeof(ofs));
2308     SetLastError(0xfaceabee);
2309     /* Create an empty file */
2310     hFile = OpenFile(filename, &ofs, OF_CREATE);
2311     ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
2312     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2313         "GetLastError() returns %d\n", GetLastError() );
2314     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2315     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2316         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2317     ret = _lclose(hFile);
2318     ok( !ret, "_lclose() returns %d\n", ret );
2319     retval = GetFileAttributesA(filename);
2320     ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
2321
2322     memset(&ofs, 0xA5, sizeof(ofs));
2323     SetLastError(0xfaceabee);
2324     /* Check various opening options: */
2325     /* for reading only, */
2326     hFile = OpenFile(filename, &ofs, OF_READ);
2327     ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
2328     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2329         "GetLastError() returns %d\n", GetLastError() );
2330     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2331     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2332         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2333     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2334         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2335     ret = _lclose(hFile);
2336     ok( !ret, "_lclose() returns %d\n", ret );
2337
2338     memset(&ofs, 0xA5, sizeof(ofs));
2339     SetLastError(0xfaceabee);
2340     /* for writing only, */
2341     hFile = OpenFile(filename, &ofs, OF_WRITE);
2342     ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
2343     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2344         "GetLastError() returns %d\n", GetLastError() );
2345     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2346     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2347         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2348     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2349         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2350     ret = _lclose(hFile);
2351     ok( !ret, "_lclose() returns %d\n", ret );
2352
2353     memset(&ofs, 0xA5, sizeof(ofs));
2354     SetLastError(0xfaceabee);
2355     /* for reading and writing, */
2356     hFile = OpenFile(filename, &ofs, OF_READWRITE);
2357     ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
2358     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2359         "GetLastError() returns %d\n", GetLastError() );
2360     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2361     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2362         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2363     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2364         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2365     ret = _lclose(hFile);
2366     ok( !ret, "_lclose() returns %d\n", ret );
2367
2368     memset(&ofs, 0xA5, sizeof(ofs));
2369     SetLastError(0xfaceabee);
2370     /* for checking file presence. */
2371     hFile = OpenFile(filename, &ofs, OF_EXIST);
2372     ok( hFile == 1, "OpenFile failed on finding our created file\n" );
2373     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2374         "GetLastError() returns %d\n", GetLastError() );
2375     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2376     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2377         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2378     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2379         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2380
2381     memset(&ofs, 0xA5, sizeof(ofs));
2382     SetLastError(0xfaceabee);
2383     /* Delete the file and make sure it doesn't exist anymore */
2384     hFile = OpenFile(filename, &ofs, OF_DELETE);
2385     ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
2386     ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS, 
2387         "GetLastError() returns %d\n", GetLastError() );
2388     ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2389     ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2390         "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2391     ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2392         "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2393
2394     retval = GetFileAttributesA(filename);
2395     ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
2396 }
2397
2398 static void test_overlapped(void)
2399 {
2400     OVERLAPPED ov;
2401     DWORD r, result;
2402
2403     /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2404     if (0) /* tested: WinXP */
2405     {
2406         GetOverlappedResult(0, NULL, &result, FALSE);
2407         GetOverlappedResult(0, &ov, NULL, FALSE);
2408         GetOverlappedResult(0, NULL, NULL, FALSE);
2409     }
2410
2411     memset( &ov, 0,  sizeof ov );
2412     result = 1;
2413     r = GetOverlappedResult(0, &ov, &result, 0);
2414     if (r)
2415         ok( result == 0, "wrong result %u\n", result );
2416     else  /* win9x */
2417         ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2418
2419     result = 0;
2420     ov.Internal = 0;
2421     ov.InternalHigh = 0xabcd;
2422     r = GetOverlappedResult(0, &ov, &result, 0);
2423     if (r)
2424         ok( result == 0xabcd, "wrong result %u\n", result );
2425     else  /* win9x */
2426         ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2427
2428     SetLastError( 0xb00 );
2429     result = 0;
2430     ov.Internal = STATUS_INVALID_HANDLE;
2431     ov.InternalHigh = 0xabcd;
2432     r = GetOverlappedResult(0, &ov, &result, 0);
2433     ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2434     ok( r == FALSE, "should return false\n");
2435     ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
2436
2437     SetLastError( 0xb00 );
2438     result = 0;
2439     ov.Internal = STATUS_PENDING;
2440     ov.InternalHigh = 0xabcd;
2441     r = GetOverlappedResult(0, &ov, &result, 0);
2442     ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2443         "wrong error %u\n", GetLastError() );
2444     ok( r == FALSE, "should return false\n");
2445     ok( result == 0, "wrong result %u\n", result );
2446
2447     SetLastError( 0xb00 );
2448     ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
2449     ov.Internal = STATUS_PENDING;
2450     ov.InternalHigh = 0xabcd;
2451     r = GetOverlappedResult(0, &ov, &result, 0);
2452     ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2453         "wrong error %u\n", GetLastError() );
2454     ok( r == FALSE, "should return false\n");
2455
2456     ResetEvent( ov.hEvent );
2457
2458     SetLastError( 0xb00 );
2459     ov.Internal = STATUS_PENDING;
2460     ov.InternalHigh = 0;
2461     r = GetOverlappedResult(0, &ov, &result, 0);
2462     ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2463         "wrong error %u\n", GetLastError() );
2464     ok( r == FALSE, "should return false\n");
2465
2466     r = CloseHandle( ov.hEvent );
2467     ok( r == TRUE, "close handle failed\n");
2468 }
2469
2470 static void test_RemoveDirectory(void)
2471 {
2472     int rc;
2473     char directory[] = "removeme";
2474
2475     rc = CreateDirectory(directory, NULL);
2476     ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
2477
2478     rc = SetCurrentDirectory(directory);
2479     ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2480
2481     rc = RemoveDirectory(".");
2482     if (!rc)
2483     {
2484         rc = SetCurrentDirectory("..");
2485         ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2486
2487         rc = RemoveDirectory(directory);
2488         ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2489     }
2490 }
2491
2492 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
2493 {
2494     ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
2495     ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
2496     return abs(t1 - t2) <= tolerance;
2497 }
2498
2499 static void test_ReplaceFileA(void)
2500 {
2501     char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2502     HANDLE hReplacedFile, hReplacementFile, hBackupFile;
2503     static const char replacedData[] = "file-to-replace";
2504     static const char replacementData[] = "new-file";
2505     static const char backupData[] = "backup-file";
2506     FILETIME ftReplaced, ftReplacement, ftBackup;
2507     static const char prefix[] = "pfx";
2508     char temp_path[MAX_PATH];
2509     DWORD ret;
2510     BOOL retok, removeBackup = FALSE;
2511
2512     if (!pReplaceFileA)
2513     {
2514         win_skip("ReplaceFileA() is missing\n");
2515         return;
2516     }
2517
2518     ret = GetTempPathA(MAX_PATH, temp_path);
2519     ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2520     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2521
2522     ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2523     ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2524
2525     ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2526     ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2527
2528     ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2529     ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2530
2531     /* place predictable data in the file to be replaced */
2532     hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2533     ok(hReplacedFile != INVALID_HANDLE_VALUE,
2534         "failed to open replaced file\n");
2535     retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2536     ok( retok && ret == sizeof(replacedData),
2537        "WriteFile error (replaced) %d\n", GetLastError());
2538     ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2539         "replaced file has wrong size\n");
2540     /* place predictable data in the file to be the replacement */
2541     hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2542     ok(hReplacementFile != INVALID_HANDLE_VALUE,
2543         "failed to open replacement file\n");
2544     retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2545     ok( retok && ret == sizeof(replacementData),
2546        "WriteFile error (replacement) %d\n", GetLastError());
2547     ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2548         "replacement file has wrong size\n");
2549     /* place predictable data in the backup file (to be over-written) */
2550     hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2551     ok(hBackupFile != INVALID_HANDLE_VALUE,
2552         "failed to open backup file\n");
2553     retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2554     ok( retok && ret == sizeof(backupData),
2555        "WriteFile error (replacement) %d\n", GetLastError());
2556     ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2557         "backup file has wrong size\n");
2558     /* change the filetime on the "replaced" file to ensure that it changes */
2559     ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2560     ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2561     ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2562     ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2563     ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2564     GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);  /* get the actual time back */
2565     CloseHandle(hReplacedFile);
2566     /* change the filetime on the backup to ensure that it changes */
2567     ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2568     ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2569     ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2570     ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2571     ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2572     GetFileTime(hBackupFile, NULL, NULL, &ftBackup);  /* get the actual time back */
2573     CloseHandle(hBackupFile);
2574     /* get the filetime on the replacement file to perform checks */
2575     ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2576     ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2577     CloseHandle(hReplacementFile);
2578
2579     /* perform replacement w/ backup
2580      * TODO: flags are not implemented
2581      */
2582     SetLastError(0xdeadbeef);
2583     ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2584     ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2585     /* make sure that the backup has the size of the old "replaced" file */
2586     hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2587     ok(hBackupFile != INVALID_HANDLE_VALUE,
2588         "failed to open backup file\n");
2589     ret = GetFileSize(hBackupFile, NULL);
2590     ok(ret == sizeof(replacedData),
2591         "backup file has wrong size %d\n", ret);
2592     /* make sure that the "replaced" file has the size of the replacement file */
2593     hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2594     ok(hReplacedFile != INVALID_HANDLE_VALUE,
2595         "failed to open replaced file: %d\n", GetLastError());
2596     if (hReplacedFile != INVALID_HANDLE_VALUE)
2597     {
2598         ret = GetFileSize(hReplacedFile, NULL);
2599         ok(ret == sizeof(replacementData),
2600             "replaced file has wrong size %d\n", ret);
2601         /* make sure that the replacement file no-longer exists */
2602         hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2603         ok(hReplacementFile == INVALID_HANDLE_VALUE,
2604            "unexpected error, replacement file should not exist %d\n", GetLastError());
2605         /* make sure that the backup has the old "replaced" filetime */
2606         ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2607         ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2608         ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
2609         CloseHandle(hBackupFile);
2610         /* make sure that the "replaced" has the old replacement filetime */
2611         ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2612         ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2613         ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
2614            "replaced file has wrong filetime %x%08x / %x%08x\n",
2615            ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2616            ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2617         CloseHandle(hReplacedFile);
2618     }
2619     else
2620         skip("couldn't open replacement file, skipping tests\n");
2621
2622     /* re-create replacement file for pass w/o backup (blank) */
2623     ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2624     ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2625     /* perform replacement w/o backup
2626      * TODO: flags are not implemented
2627      */
2628     SetLastError(0xdeadbeef);
2629     ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2630     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2631        "ReplaceFileA: unexpected error %d\n", GetLastError());
2632
2633     /* re-create replacement file for pass w/ backup (backup-file not existing) */
2634     ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2635     ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2636     ret = DeleteFileA(backup);
2637     ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2638     /* perform replacement w/ backup (no pre-existing backup)
2639      * TODO: flags are not implemented
2640      */
2641     SetLastError(0xdeadbeef);
2642     ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2643     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2644        "ReplaceFileA: unexpected error %d\n", GetLastError());
2645     if (ret)
2646         removeBackup = TRUE;
2647
2648     /* re-create replacement file for pass w/ no permissions to "replaced" */
2649     ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2650     ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2651     ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2652     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2653        "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2654     /* perform replacement w/ backup (no permission to "replaced")
2655      * TODO: flags are not implemented
2656      */
2657     SetLastError(0xdeadbeef);
2658     ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2659     ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2660     /* make sure that the replacement file still exists */
2661     hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2662     ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2663        broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2664        "unexpected error, replacement file should still exist %d\n", GetLastError());
2665     CloseHandle(hReplacementFile);
2666     ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2667     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2668        "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2669
2670     /* replacement file still exists, make pass w/o "replaced" */
2671     ret = DeleteFileA(replaced);
2672     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2673        "DeleteFileA: error (replaced) %d\n", GetLastError());
2674     /* perform replacement w/ backup (no pre-existing backup or "replaced")
2675      * TODO: flags are not implemented
2676      */
2677     SetLastError(0xdeadbeef);
2678     ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2679     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2680        GetLastError() == ERROR_ACCESS_DENIED),
2681        "ReplaceFileA: unexpected error %d\n", GetLastError());
2682
2683     /* perform replacement w/o existing "replacement" file
2684      * TODO: flags are not implemented
2685      */
2686     SetLastError(0xdeadbeef);
2687     ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2688     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2689         GetLastError() == ERROR_ACCESS_DENIED),
2690         "ReplaceFileA: unexpected error %d\n", GetLastError());
2691
2692     /*
2693      * if the first round (w/ backup) worked then as long as there is no
2694      * failure then there is no need to check this round (w/ backup is the
2695      * more complete case)
2696      */
2697
2698     /* delete temporary files, replacement and replaced are already deleted */
2699     if (removeBackup)
2700     {
2701         ret = DeleteFileA(backup);
2702         ok(ret ||
2703            broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2704            "DeleteFileA: error (backup) %d\n", GetLastError());
2705     }
2706 }
2707
2708 /*
2709  * ReplaceFileW is a simpler case of ReplaceFileA, there is no
2710  * need to be as thorough.
2711  */
2712 static void test_ReplaceFileW(void)
2713 {
2714     WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2715     static const WCHAR prefix[] = {'p','f','x',0};
2716     WCHAR temp_path[MAX_PATH];
2717     DWORD ret;
2718     BOOL removeBackup = FALSE;
2719
2720     if (!pReplaceFileW)
2721     {
2722         win_skip("ReplaceFileW() is missing\n");
2723         return;
2724     }
2725
2726     ret = GetTempPathW(MAX_PATH, temp_path);
2727     if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2728     {
2729         win_skip("GetTempPathW is not available\n");
2730         return;
2731     }
2732     ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
2733     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2734
2735     ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
2736     ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
2737
2738     ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2739     ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2740
2741     ret = GetTempFileNameW(temp_path, prefix, 0, backup);
2742     ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
2743
2744     ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2745     ok(ret, "ReplaceFileW: error %d\n", GetLastError());
2746
2747     ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2748     ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2749     ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2750     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2751        "ReplaceFileW: error %d\n", GetLastError());
2752
2753     ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2754     ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2755     ret = DeleteFileW(backup);
2756     ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
2757     ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2758     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2759        "ReplaceFileW: error %d\n", GetLastError());
2760
2761     ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
2762     ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
2763     ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
2764     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2765        "SetFileAttributesW: error setting to read only %d\n", GetLastError());
2766
2767     ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2768     ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
2769         "ReplaceFileW: unexpected error %d\n", GetLastError());
2770     ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
2771     ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2772        "SetFileAttributesW: error setting to normal %d\n", GetLastError());
2773     if (ret)
2774         removeBackup = TRUE;
2775
2776     ret = DeleteFileW(replaced);
2777     ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
2778     ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
2779     ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
2780
2781     ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
2782     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
2783        GetLastError() == ERROR_ACCESS_DENIED),
2784         "ReplaceFileW: unexpected error %d\n", GetLastError());
2785
2786     if (removeBackup)
2787     {
2788         ret = DeleteFileW(backup);
2789         ok(ret ||
2790            broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
2791            "DeleteFileW: error (backup) %d\n", GetLastError());
2792     }
2793 }
2794
2795 START_TEST(file)
2796 {
2797     InitFunctionPointers();
2798
2799     test__hread(  );
2800     test__hwrite(  );
2801     test__lclose(  );
2802     test__lcreat(  );
2803     test__llseek(  );
2804     test__llopen(  );
2805     test__lread(  );
2806     test__lwrite(  );
2807     test_GetTempFileNameA();
2808     test_CopyFileA();
2809     test_CopyFileW();
2810     test_CreateFileA();
2811     test_CreateFileW();
2812     test_DeleteFileA();
2813     test_DeleteFileW();
2814     test_MoveFileA();
2815     test_MoveFileW();
2816     test_FindFirstFileA();
2817     test_FindNextFileA();
2818     test_FindFirstFileExA(0);
2819     /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
2820     test_FindFirstFileExA(FindExSearchLimitToDirectories);
2821     test_LockFile();
2822     test_file_sharing();
2823     test_offset_in_overlapped_structure();
2824     test_MapFile();
2825     test_GetFileType();
2826     test_async_file_errors();
2827     test_read_write();
2828     test_OpenFile();
2829     test_overlapped();
2830     test_RemoveDirectory();
2831     test_ReplaceFileA();
2832     test_ReplaceFileW();
2833 }