~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/kernel32/tests/file.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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