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