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

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

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.