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