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