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