Implements OleLoadPicturePath.
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
40 LPCSTR filename = "testfile.xxx";
41 LPCSTR 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
54 static void test__hread( void )
55 {
56     HFILE filehandle;
57     char buffer[10000];
58     long bytes_read;
59     long bytes_wanted;
60     long i;
61     BOOL ret;
62
63     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
64     DeleteFileA( filename );
65     filehandle = _lcreat( filename, 0 );
66     if (filehandle == HFILE_ERROR)
67     {
68         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
69         return;
70     }
71
72     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
73
74     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
75
76     filehandle = _lopen( filename, OF_READ );
77
78     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError(  ) );
79
80     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
81
82     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
83
84     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
85     {
86         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
87         ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
88         for (i = 0; i < bytes_wanted; i++)
89         {
90             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
91         }
92     }
93
94     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
95
96     ret = DeleteFileA( filename );
97     ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError(  ) );
98 }
99
100
101 static void test__hwrite( void )
102 {
103     HFILE filehandle;
104     char buffer[10000];
105     long bytes_read;
106     long bytes_written;
107     long blocks;
108     long i;
109     char *contents;
110     HLOCAL memory_object;
111     char checksum[1];
112     BOOL ret;
113
114     filehandle = _lcreat( filename, 0 );
115     if (filehandle == HFILE_ERROR)
116     {
117         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
118         return;
119     }
120
121     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
122
123     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
124
125     filehandle = _lopen( filename, OF_READ );
126
127     bytes_read = _hread( filehandle, buffer, 1);
128
129     ok( 0 == bytes_read, "file read size error\n" );
130
131     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
132
133     filehandle = _lopen( filename, OF_READWRITE );
134
135     bytes_written = 0;
136     checksum[0] = '\0';
137     srand( (unsigned)time( NULL ) );
138     for (blocks = 0; blocks < 100; blocks++)
139     {
140         for (i = 0; i < (long)sizeof( buffer ); i++)
141         {
142             buffer[i] = rand(  );
143             checksum[0] = checksum[0] + buffer[i];
144         }
145         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
146         bytes_written = bytes_written + sizeof( buffer );
147     }
148
149     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
150     bytes_written++;
151
152     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
153
154     memory_object = LocalAlloc( LPTR, bytes_written );
155
156     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
157
158     contents = LocalLock( memory_object );
159
160     filehandle = _lopen( filename, OF_READ );
161
162     contents = LocalLock( memory_object );
163
164     ok( NULL != contents, "LocalLock whines\n" );
165
166     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
167
168     checksum[0] = '\0';
169     i = 0;
170     do
171     {
172         checksum[0] = checksum[0] + contents[i];
173         i++;
174     }
175     while (i < bytes_written - 1);
176
177     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
178
179     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
180
181     ret = DeleteFileA( filename );
182     ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError(  ) );
183 }
184
185
186 static void test__lclose( void )
187 {
188     HFILE filehandle;
189     BOOL ret;
190
191     filehandle = _lcreat( filename, 0 );
192     if (filehandle == HFILE_ERROR)
193     {
194         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
195         return;
196     }
197
198     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
199
200     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
201
202     ret = DeleteFileA( filename );
203     ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError(  ) );
204 }
205
206
207 static void test__lcreat( void )
208 {
209     HFILE filehandle;
210     char buffer[10000];
211     WIN32_FIND_DATAA search_results;
212     char slashname[] = "testfi/";
213     int err;
214     HANDLE find;
215     BOOL ret;
216
217     filehandle = _lcreat( filename, 0 );
218     if (filehandle == HFILE_ERROR)
219     {
220         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
221         return;
222     }
223
224     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
225
226     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
227
228     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
229
230     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
231
232     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
233
234     ret = DeleteFileA(filename);
235     ok( ret != 0, "DeleteFile failed (%ld)\n", GetLastError());
236
237     filehandle = _lcreat( filename, 1 ); /* readonly */
238     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError(  ) );
239
240     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
241
242     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
243
244     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
245
246     ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
247
248     ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
249
250     ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
251
252     filehandle = _lcreat( filename, 2 );
253     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError(  ) );
254
255     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
256
257     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
258
259     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
260
261     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
262
263     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
264
265     ret = DeleteFileA( filename );
266     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
267
268     filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
269     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError(  ) );
270
271     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
272
273     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
274
275     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value\n" );
276
277     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
278
279     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
280
281     ret = DeleteFileA( filename );
282     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
283
284     filehandle=_lcreat (slashname, 0); /* illegal name */
285     if (HFILE_ERROR==filehandle) {
286       err=GetLastError ();
287       ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
288           "creating file \"%s\" failed with error %d\n", slashname, err);
289     } else { /* only NT succeeds */
290       _lclose(filehandle);
291       find=FindFirstFileA (slashname, &search_results);
292       if (INVALID_HANDLE_VALUE!=find)
293       {
294         ret = FindClose (find);
295         ok (0 != ret, "FindClose complains (%ld)\n", GetLastError ());
296         slashname[strlen(slashname)-1]=0;
297         ok (!strcmp (slashname, search_results.cFileName),
298             "found unexpected name \"%s\"\n", search_results.cFileName);
299         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
300             "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
301             search_results.dwFileAttributes);
302       }
303     ret = DeleteFileA( slashname );
304     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
305     }
306
307     filehandle=_lcreat (filename, 8); /* illegal attribute */
308     if (HFILE_ERROR==filehandle)
309       ok (0, "couldn't create volume label \"%s\"\n", filename);
310     else {
311       _lclose(filehandle);
312       find=FindFirstFileA (filename, &search_results);
313       if (INVALID_HANDLE_VALUE==find)
314         ok (0, "file \"%s\" not found\n", filename);
315       else {
316         ret = FindClose(find);
317         ok ( 0 != ret, "FindClose complains (%ld)\n", GetLastError ());
318         ok (!strcmp (filename, search_results.cFileName),
319             "found unexpected name \"%s\"\n", search_results.cFileName);
320         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
321             "attributes of file \"%s\" are 0x%04lx\n", search_results.cFileName,
322             search_results.dwFileAttributes);
323       }
324     ret = DeleteFileA( filename );
325     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
326     }
327 }
328
329
330 static void test__llseek( void )
331 {
332     INT i;
333     HFILE filehandle;
334     char buffer[1];
335     long bytes_read;
336     BOOL ret;
337
338     filehandle = _lcreat( filename, 0 );
339     if (filehandle == HFILE_ERROR)
340     {
341         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
342         return;
343     }
344
345     for (i = 0; i < 400; i++)
346     {
347         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
348     }
349     ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
350     ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
351
352     bytes_read = _hread( filehandle, buffer, 1);
353     ok( 1 == bytes_read, "file read size error\n" );
354     ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
355     ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
356
357     bytes_read = _hread( filehandle, buffer, 1);
358     ok( 1 == bytes_read, "file read size error\n" );
359     ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
360     ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
361     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
362
363     ret = DeleteFileA( filename );
364     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
365 }
366
367
368 static void test__llopen( void )
369 {
370     HFILE filehandle;
371     UINT bytes_read;
372     char buffer[10000];
373     BOOL ret;
374
375     filehandle = _lcreat( filename, 0 );
376     if (filehandle == HFILE_ERROR)
377     {
378         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
379         return;
380     }
381
382     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
383     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
384
385     filehandle = _lopen( filename, OF_READ );
386     ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
387     bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
388     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
389     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
390
391     filehandle = _lopen( filename, OF_READWRITE );
392     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
393     ok( strlen( sillytext )  == bytes_read, "file read size error\n" );
394     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
395     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
396
397     filehandle = _lopen( filename, OF_WRITE );
398     ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
399     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
400     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
401
402     ret = DeleteFileA( filename );
403     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
404     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
405 }
406
407
408 static void test__lread( void )
409 {
410     HFILE filehandle;
411     char buffer[10000];
412     long bytes_read;
413     UINT bytes_wanted;
414     UINT i;
415     BOOL ret;
416
417     filehandle = _lcreat( filename, 0 );
418     if (filehandle == HFILE_ERROR)
419     {
420         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
421         return;
422     }
423
424     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
425
426     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
427
428     filehandle = _lopen( filename, OF_READ );
429
430     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)\n", filename, GetLastError());
431
432     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
433
434     ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
435
436     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
437     {
438         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
439         ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
440         for (i = 0; i < bytes_wanted; i++)
441         {
442             ok( buffer[i] == sillytext[i], "that's not what's written\n" );
443         }
444     }
445
446     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
447
448     ret = DeleteFileA( filename );
449     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
450 }
451
452
453 static void test__lwrite( void )
454 {
455     HFILE filehandle;
456     char buffer[10000];
457     long bytes_read;
458     long bytes_written;
459     long blocks;
460     long i;
461     char *contents;
462     HLOCAL memory_object;
463     char checksum[1];
464     BOOL ret;
465
466     filehandle = _lcreat( filename, 0 );
467     if (filehandle == HFILE_ERROR)
468     {
469         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
470         return;
471     }
472
473     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
474
475     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
476
477     filehandle = _lopen( filename, OF_READ );
478
479     bytes_read = _hread( filehandle, buffer, 1);
480
481     ok( 0 == bytes_read, "file read size error\n" );
482
483     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
484
485     filehandle = _lopen( filename, OF_READWRITE );
486
487     bytes_written = 0;
488     checksum[0] = '\0';
489     srand( (unsigned)time( NULL ) );
490     for (blocks = 0; blocks < 100; blocks++)
491     {
492         for (i = 0; i < (long)sizeof( buffer ); i++)
493         {
494             buffer[i] = rand(  );
495             checksum[0] = checksum[0] + buffer[i];
496         }
497         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
498         bytes_written = bytes_written + sizeof( buffer );
499     }
500
501     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
502     bytes_written++;
503
504     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
505
506     memory_object = LocalAlloc( LPTR, bytes_written );
507
508     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
509
510     contents = LocalLock( memory_object );
511
512     filehandle = _lopen( filename, OF_READ );
513
514     contents = LocalLock( memory_object );
515
516     ok( NULL != contents, "LocalLock whines\n" );
517
518     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
519
520     checksum[0] = '\0';
521     i = 0;
522     do
523     {
524         checksum[0] += contents[i];
525         i++;
526     }
527     while (i < bytes_written - 1);
528
529     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
530
531     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
532
533     ret = DeleteFileA( filename );
534     ok( ret, "DeleteFile failed (%ld)\n", GetLastError(  ) );
535 }
536
537 static void test_CopyFileA(void)
538 {
539     char temp_path[MAX_PATH];
540     char source[MAX_PATH], dest[MAX_PATH];
541     static const char prefix[] = "pfx";
542     HANDLE hfile;
543     FILETIME ft1, ft2;
544     char buf[10];
545     DWORD ret;
546     BOOL retok;
547
548     ret = GetTempPathA(MAX_PATH, temp_path);
549     ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
550     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
551
552     ret = GetTempFileNameA(temp_path, prefix, 0, source);
553     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
554
555     /* make the source have not zero size */
556     hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
557     ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
558     retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
559     ok( retok && ret == sizeof(prefix),
560        "WriteFile error %ld\n", GetLastError());
561     ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
562     /* get the file time and change it to prove the difference */
563     ok(GetFileTime(hfile, NULL, NULL, &ft1), "GetFileTime error %ld\n", GetLastError());
564     ft1.dwLowDateTime -= 600000000; /* 60 second */
565     ok(SetFileTime(hfile, NULL, NULL, &ft1), "SetFileTime error %ld\n", GetLastError());
566     CloseHandle(hfile);
567
568     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
569     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
570
571     SetLastError(0xdeadbeef);
572     ret = CopyFileA(source, dest, TRUE);
573     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
574        "CopyFileA: unexpected error %ld\n", GetLastError());
575
576     ret = CopyFileA(source, dest, FALSE);
577     ok(ret, "CopyFileA: error %ld\n", GetLastError());
578
579     /* make sure that destination has correct size */
580     hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
581     ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
582     ret = GetFileSize(hfile, NULL);
583     ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
584
585     /* make sure that destination has the same filetime */
586     ok(ret = GetFileTime(hfile, NULL, NULL, &ft1), "GetFileTime error %ld\n", GetLastError());
587     ok(ret = GetFileTime(hfile, NULL, NULL, &ft2), "GetFileTime error %ld\n", GetLastError());
588     ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
589
590     SetLastError(0xdeadbeef);
591     ret = CopyFileA(source, dest, FALSE);
592     ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
593        "CopyFileA: ret = %ld, unexpected error %ld\n", ret, GetLastError());
594
595     /* make sure that destination still has correct size */
596     ret = GetFileSize(hfile, NULL);
597     ok(ret == sizeof(prefix), "destination file has wrong size %ld\n", ret);
598     retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
599     ok( retok && ret == sizeof(prefix),
600        "ReadFile: error %ld\n", GetLastError());
601     ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
602     CloseHandle(hfile);
603
604     ret = DeleteFileA(source);
605     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
606     ret = DeleteFileA(dest);
607     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
608 }
609
610 static void test_CopyFileW(void)
611 {
612     WCHAR temp_path[MAX_PATH];
613     WCHAR source[MAX_PATH], dest[MAX_PATH];
614     static const WCHAR prefix[] = {'p','f','x',0};
615     DWORD ret;
616
617     ret = GetTempPathW(MAX_PATH, temp_path);
618     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
619         return;
620     ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
621     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
622
623     ret = GetTempFileNameW(temp_path, prefix, 0, source);
624     ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
625
626     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
627     ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
628
629     ret = CopyFileW(source, dest, TRUE);
630     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
631        "CopyFileW: unexpected error %ld\n", GetLastError());
632
633     ret = CopyFileW(source, dest, FALSE);
634     ok(ret, "CopyFileW: error %ld\n", GetLastError());
635
636     ret = DeleteFileW(source);
637     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
638     ret = DeleteFileW(dest);
639     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
640 }
641
642 static void test_CreateFileA(void)
643 {
644     HANDLE hFile;
645     char temp_path[MAX_PATH];
646     char filename[MAX_PATH];
647     static const char prefix[] = "pfx";
648     DWORD ret;
649
650     ret = GetTempPathA(MAX_PATH, temp_path);
651     ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
652     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
653
654     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
655     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
656
657     hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
658                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
659     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
660         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
661
662     ret = DeleteFileA(filename);
663     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
664 }
665
666 static void test_CreateFileW(void)
667 {
668     HANDLE hFile;
669     WCHAR temp_path[MAX_PATH];
670     WCHAR filename[MAX_PATH];
671     static const WCHAR emptyW[]={'\0'};
672     static const WCHAR prefix[] = {'p','f','x',0};
673     static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
674     DWORD ret;
675
676     ret = GetTempPathW(MAX_PATH, temp_path);
677     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
678         return;
679     ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
680     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
681
682     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
683     ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
684
685     hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
686                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
687     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
688         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
689
690     ret = DeleteFileW(filename);
691     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
692
693     hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
694                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
695     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
696        "CreateFileW(NULL) returned ret=%p error=%ld\n",hFile,GetLastError());
697
698     hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
699                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
700     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
701        "CreateFileW(\"\") returned ret=%p error=%ld\n",hFile,GetLastError());
702
703     /* test the result of opening a nonexistent driver name */
704     hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
705                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
706     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
707        "CreateFileW on invalid VxD name returned ret=%p error=%ld\n",hFile,GetLastError());
708 }
709
710 static void test_GetTempFileNameA()
711 {
712     UINT result;
713     char out[MAX_PATH];
714     char expected[MAX_PATH + 10];
715     char windowsdir[MAX_PATH + 10];
716     char windowsdrive[3];
717
718     result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
719     ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
720     ok(result != 0, "GetWindowsDirectory: error %ld\n", GetLastError());
721
722     /* If the Windows directory is the root directory, it ends in backslash, not else. */
723     if (strlen(windowsdir) != 3) /* As in  "C:\"  or  "F:\"  */
724     {
725         strcat(windowsdir, "\\");
726     }
727
728     windowsdrive[0] = windowsdir[0];
729     windowsdrive[1] = windowsdir[1];
730     windowsdrive[2] = '\0';
731
732     result = GetTempFileNameA(windowsdrive, "abc", 1, out);
733     ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
734     ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
735        "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
736        windowsdrive[0], out);
737
738     result = GetTempFileNameA(windowsdir, "abc", 2, out);
739     ok(result != 0, "GetTempFileNameA: error %ld\n", GetLastError());
740     expected[0] = '\0';
741     strcat(expected, windowsdir);
742     strcat(expected, "abc2.tmp");
743     ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
744        out, expected);
745 }
746
747 static void test_DeleteFileA( void )
748 {
749     BOOL ret;
750
751     ret = DeleteFileA(NULL);
752     ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
753                 GetLastError() == ERROR_PATH_NOT_FOUND),
754        "DeleteFileA(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
755
756     ret = DeleteFileA("");
757     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
758                 GetLastError() == ERROR_BAD_PATHNAME),
759        "DeleteFileA(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
760
761     ret = DeleteFileA("nul");
762     ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
763                 GetLastError() == ERROR_INVALID_PARAMETER ||
764                 GetLastError() == ERROR_ACCESS_DENIED),
765        "DeleteFileA(\"nul\") returned ret=%d error=%ld\n",ret,GetLastError());
766 }
767
768 static void test_DeleteFileW( void )
769 {
770     BOOL ret;
771     static const WCHAR emptyW[]={'\0'};
772
773     ret = DeleteFileW(NULL);
774     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
775         return;
776     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
777        "DeleteFileW(NULL) returned ret=%d error=%ld\n",ret,GetLastError());
778
779     ret = DeleteFileW(emptyW);
780     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
781        "DeleteFileW(\"\") returned ret=%d error=%ld\n",ret,GetLastError());
782 }
783
784 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
785
786 static void test_MoveFileA(void)
787 {
788     char tempdir[MAX_PATH];
789     char source[MAX_PATH], dest[MAX_PATH];
790     static const char prefix[] = "pfx";
791     DWORD ret;
792
793     ret = GetTempPathA(MAX_PATH, tempdir);
794     ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
795     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
796
797     ret = GetTempFileNameA(tempdir, prefix, 0, source);
798     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
799
800     ret = GetTempFileNameA(tempdir, prefix, 0, dest);
801     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
802
803     ret = MoveFileA(source, dest);
804     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
805        "MoveFileA: unexpected error %ld\n", GetLastError());
806
807     ret = DeleteFileA(dest);
808     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
809
810     ret = MoveFileA(source, dest);
811     ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
812
813     lstrcatA(tempdir, "Remove Me");
814     ret = CreateDirectoryA(tempdir, NULL);
815     ok(ret == TRUE, "CreateDirectoryA failed\n");
816
817     lstrcpyA(source, dest);
818     lstrcpyA(dest, tempdir);
819     lstrcatA(dest, "\\wild?.*");
820     /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
821     ret = MoveFileA(source, dest);
822     ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
823     ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
824        GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
825        "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
826     if (ret || (GetLastError() != ERROR_INVALID_NAME))
827     {
828         WIN32_FIND_DATAA fd;
829         char temppath[MAX_PATH];
830         HANDLE hFind;
831
832         lstrcpyA(temppath, tempdir);
833         lstrcatA(temppath, "\\*.*");
834         hFind = FindFirstFileA(temppath, &fd);
835         if (INVALID_HANDLE_VALUE != hFind)
836         {
837           LPSTR lpName;
838           do
839           {
840             lpName = fd.cAlternateFileName;
841             if (!lpName[0])
842               lpName = fd.cFileName;
843             ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
844           }
845           while (FindNextFileA(hFind, &fd));
846           FindClose(hFind);
847         }
848     }
849     ret = DeleteFileA(source);
850     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
851     ret = DeleteFileA(dest);
852     ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
853     ret = RemoveDirectoryA(tempdir);
854     ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
855 }
856
857 static void test_MoveFileW(void)
858 {
859     WCHAR temp_path[MAX_PATH];
860     WCHAR source[MAX_PATH], dest[MAX_PATH];
861     static const WCHAR prefix[] = {'p','f','x',0};
862     DWORD ret;
863
864     ret = GetTempPathW(MAX_PATH, temp_path);
865     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
866         return;
867     ok(ret != 0, "GetTempPathW error %ld\n", GetLastError());
868     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
869
870     ret = GetTempFileNameW(temp_path, prefix, 0, source);
871     ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
872
873     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
874     ok(ret != 0, "GetTempFileNameW error %ld\n", GetLastError());
875
876     ret = MoveFileW(source, dest);
877     ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
878        "CopyFileW: unexpected error %ld\n", GetLastError());
879
880     ret = DeleteFileW(source);
881     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
882     ret = DeleteFileW(dest);
883     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
884 }
885
886 #define PATTERN_OFFSET 0x10
887
888 static void test_offset_in_overlapped_structure(void)
889 {
890     HANDLE hFile;
891     OVERLAPPED ov;
892     DWORD done;
893     BOOL rc;
894     BYTE buf[256], pattern[] = "TeSt";
895     UINT i;
896     char temp_path[MAX_PATH], temp_fname[MAX_PATH];
897     BOOL ret;
898
899     ret =GetTempPathA(MAX_PATH, temp_path);
900     ok( ret, "GetTempPathA error %ld\n", GetLastError());
901     ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
902     ok( ret, "GetTempFileNameA error %ld\n", GetLastError());
903
904     /*** Write File *****************************************************/
905
906     hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
907     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
908
909     for(i = 0; i < sizeof(buf); i++) buf[i] = i;
910     ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
911     ok( ret, "WriteFile error %ld\n", GetLastError());
912     ok(done == sizeof(buf), "expected number of bytes written %lu\n", done);
913
914     memset(&ov, 0, sizeof(ov));
915     ov.Offset = PATTERN_OFFSET;
916     ov.OffsetHigh = 0;
917     rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
918     /* Win 9x does not support the overlapped I/O on files */
919     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
920         ok(rc, "WriteFile error %ld\n", GetLastError());
921         ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
922         /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
923         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
924            "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
925
926         ov.Offset = sizeof(buf) * 2;
927         ov.OffsetHigh = 0;
928         ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
929         ok( ret, "WriteFile error %ld\n", GetLastError());
930         ok(done == sizeof(pattern), "expected number of bytes written %lu\n", done);
931         /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
932         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
933            "expected file offset %d\n", sizeof(buf) * 2 + sizeof(pattern));
934     }
935
936     CloseHandle(hFile);
937
938     /*** Read File *****************************************************/
939
940     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
941     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError());
942
943     memset(buf, 0, sizeof(buf));
944     memset(&ov, 0, sizeof(ov));
945     ov.Offset = PATTERN_OFFSET;
946     ov.OffsetHigh = 0;
947     rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
948     /* Win 9x does not support the overlapped I/O on files */
949     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
950         ok(rc, "ReadFile error %ld\n", GetLastError());
951         ok(done == sizeof(pattern), "expected number of bytes read %lu\n", done);
952         /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
953         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
954            "expected file offset %d\n", PATTERN_OFFSET + sizeof(pattern));
955         ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
956     }
957
958     CloseHandle(hFile);
959
960     ret = DeleteFileA(temp_fname);
961     ok( ret, "DeleteFileA error %ld\n", GetLastError());
962 }
963
964 static void test_LockFile(void)
965 {
966     HANDLE handle;
967     DWORD written;
968     OVERLAPPED overlapped;
969     int limited_LockFile;
970     int limited_UnLockFile;
971     int lockfileex_capable;
972
973     handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
974                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
975                           CREATE_ALWAYS, 0, 0 );
976     if (handle == INVALID_HANDLE_VALUE)
977     {
978         ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
979         return;
980     }
981     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
982
983     ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
984     ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
985
986     limited_UnLockFile = 0;
987     if (UnlockFile( handle, 0, 0, 0, 0 ))
988     {
989         limited_UnLockFile = 1;
990     }
991
992     ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
993     /* overlapping locks must fail */
994     ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
995     ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
996     /* non-overlapping locks must succeed */
997     ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
998
999     ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1000     ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1001     ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1002     ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1003
1004     overlapped.Offset = 100;
1005     overlapped.OffsetHigh = 0;
1006     overlapped.hEvent = 0;
1007
1008     lockfileex_capable = dll_capable("kernel32", "LockFileEx");
1009     if (lockfileex_capable)
1010     {
1011         /* Test for broken LockFileEx a la Windows 95 OSR2. */
1012         if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1013         {
1014             /* LockFileEx is probably OK, test it more. */
1015             ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1016                 "LockFileEx 100,100 failed\n" );
1017         }
1018     }
1019
1020     /* overlapping shared locks are OK */
1021     overlapped.Offset = 150;
1022     limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1023
1024     /* but exclusive is not */
1025     if (lockfileex_capable)
1026     {
1027         ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1028                          0, 50, 0, &overlapped ),
1029                          "LockFileEx exclusive 150,50 succeeded\n" );
1030         if (dll_capable("kernel32.dll", "UnlockFileEx"))
1031         {
1032             if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1033             { /* UnLockFile is capable. */
1034                 overlapped.Offset = 100;
1035                 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1036                     "UnlockFileEx 150,100 again succeeded\n" );
1037             }
1038         }
1039     }
1040
1041     ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed\n" );
1042     ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1043     ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1044     ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1045
1046     /* wrap-around lock should not do anything */
1047     /* (but still succeeds on NT4 so we don't check result) */
1048     LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1049
1050     limited_LockFile = 0;
1051     if (!LockFile( handle, ~0, ~0, 1, 0 ))
1052     {
1053         limited_LockFile = 1;
1054     }
1055
1056     limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1057
1058     /* zero-byte lock */
1059     ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1060     limited_LockFile || ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1061     ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1062     limited_LockFile || ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1063
1064     ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1065     !ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed\n" );
1066
1067     ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1068
1069     CloseHandle( handle );
1070     DeleteFileA( filename );
1071 }
1072
1073 static inline int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
1074 {
1075     if (!access1 || !access2) return 1;
1076     if ((access1 & GENERIC_READ) && !(sharing2 & FILE_SHARE_READ)) return 0;
1077     if ((access1 & GENERIC_WRITE) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1078     if ((access2 & GENERIC_READ) && !(sharing1 & FILE_SHARE_READ)) return 0;
1079     if ((access2 & GENERIC_WRITE) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1080     return 1;
1081 }
1082
1083 static void test_file_sharing(void)
1084 {
1085     static const DWORD access_modes[4] = { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE };
1086     static const DWORD sharing_modes[4] = { 0, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE };
1087     int a1, s1, a2, s2;
1088     int ret;
1089     HANDLE h, h2;
1090
1091     /* make sure the file exists */
1092     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1093     if (h == INVALID_HANDLE_VALUE)
1094     {
1095         ok(0, "couldn't create file \"%s\" (err=%ld)\n", filename, GetLastError());
1096         return;
1097     }
1098     CloseHandle( h );
1099
1100     for (a1 = 0; a1 < 4; a1++)
1101     {
1102         for (s1 = 0; s1 < 4; s1++)
1103         {
1104             h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1105                              NULL, OPEN_EXISTING, 0, 0 );
1106             if (h == INVALID_HANDLE_VALUE)
1107             {
1108                 ok(0,"couldn't create file \"%s\" (err=%ld)\n",filename,GetLastError());
1109                 return;
1110             }
1111             for (a2 = 0; a2 < 4; a2++)
1112             {
1113                 for (s2 = 0; s2 < 4; s2++)
1114                 {
1115                     SetLastError(0xdeadbeef);
1116                     h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1117                                       NULL, OPEN_EXISTING, 0, 0 );
1118                     if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1119                                                access_modes[a2], sharing_modes[s2] ))
1120                     {
1121                         ret = GetLastError();
1122                         ok( ERROR_SHARING_VIOLATION == ret || 0 == ret,
1123                             "Windows 95 sets GetLastError() = ERROR_SHARING_VIOLATION and\n"
1124                             "  Windows XP GetLastError() = 0, but now it is %d.\n"
1125                             "  indexes = %d, %d, %d, %d\n"
1126                             "  modes   =\n  %lx/%lx/%lx/%lx\n",
1127                             ret,
1128                             a1, s1, a2, s2, 
1129                             access_modes[a1], sharing_modes[s1],
1130                             access_modes[a2], sharing_modes[s2]
1131                             );
1132                     }
1133                     else
1134                     {
1135                         ok( h2 == INVALID_HANDLE_VALUE,
1136                             "open succeeded for modes %lx/%lx/%lx/%lx\n",
1137                             access_modes[a1], sharing_modes[s1],
1138                             access_modes[a2], sharing_modes[s2] );
1139                         if (h2 == INVALID_HANDLE_VALUE)
1140                             ok( GetLastError() == ERROR_SHARING_VIOLATION,
1141                                 "wrong error code %ld\n", GetLastError() );
1142                     }
1143                     if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1144                 }
1145             }
1146             CloseHandle( h );
1147         }
1148     }
1149
1150     SetLastError(0xdeadbeef);
1151     h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1152     ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError() );
1153
1154     SetLastError(0xdeadbeef);
1155     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1156     ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1157     ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %ld\n", GetLastError() );
1158
1159     h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1160     ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %ld\n", GetLastError() );
1161
1162     CloseHandle(h);
1163     CloseHandle(h2);
1164
1165     DeleteFileA( filename );
1166 }
1167
1168 static char get_windows_drive()
1169 {
1170     char windowsdir[MAX_PATH];
1171     GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1172     return windowsdir[0];
1173 }
1174
1175 static void test_FindFirstFileA()
1176 {
1177     HANDLE handle;
1178     WIN32_FIND_DATAA search_results;
1179     int err;
1180     char buffer[5] = "C:\\";
1181
1182     /* try FindFirstFileA on "C:\" */
1183     buffer[0] = get_windows_drive();
1184     handle = FindFirstFileA(buffer,&search_results);
1185     err = GetLastError();
1186     ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on root directory should Fail\n");
1187     if (handle == INVALID_HANDLE_VALUE)
1188       ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
1189
1190     /* try FindFirstFileA on "C:\*" */
1191     strcat(buffer, "*");
1192     handle = FindFirstFileA(buffer,&search_results);
1193     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer );
1194     ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1195 }
1196
1197 static void test_FindNextFileA()
1198 {
1199     HANDLE handle;
1200     WIN32_FIND_DATAA search_results;
1201     int err;
1202     char buffer[5] = "C:\\*";
1203
1204     buffer[0] = get_windows_drive();
1205     handle = FindFirstFileA(buffer,&search_results);
1206     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
1207     while (FindNextFile(handle, &search_results))
1208     {
1209         /* get to the end of the files */
1210     }
1211     ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
1212     err = GetLastError();
1213     ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
1214 }
1215
1216 static int test_Mapfile_createtemp(HANDLE *handle)
1217 {
1218     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
1219     DeleteFile(filename);
1220     *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
1221                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1222     if (*handle != INVALID_HANDLE_VALUE) {
1223
1224         return 1;
1225     }
1226
1227     return 0;
1228 }
1229
1230 static void test_MapFile()
1231 {
1232     HANDLE handle;
1233     HANDLE hmap;
1234
1235     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1236
1237     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
1238     ok( hmap != NULL, "mapping should work, I named it!\n" );
1239
1240     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1241
1242     /* We have to close file before we try new stuff with mapping again.
1243        Else we would always succeed on XP or block descriptors on 95. */
1244     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1245     ok( hmap != NULL, "We should still be able to map!\n" );
1246     ok( CloseHandle( hmap ), "can't close mapping handle\n");
1247     ok( CloseHandle( handle ), "can't close file handle\n");
1248     handle = NULL;
1249
1250     ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
1251
1252     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
1253     ok( hmap == NULL, "mapped zero size file\n");
1254     ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
1255
1256     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
1257     ok( hmap == NULL, "mapping should fail\n");
1258     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1259
1260     hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
1261     ok( hmap == NULL, "mapping should fail\n");
1262     /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
1263
1264     /* On XP you can now map again, on Win 95 you can not. */
1265
1266     ok( CloseHandle( handle ), "can't close file handle\n");
1267     ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
1268 }
1269
1270 static void test_GetFileType(void)
1271 {
1272     DWORD type;
1273     HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1274     ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
1275     type = GetFileType(h);
1276     ok( type == FILE_TYPE_DISK, "expected type disk got %ld\n", type );
1277     CloseHandle( h );
1278     h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1279     ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
1280     type = GetFileType(h);
1281     ok( type == FILE_TYPE_CHAR, "expected type char for nul got %ld\n", type );
1282     CloseHandle( h );
1283     DeleteFileA( filename );
1284 }
1285
1286 static int completion_count;
1287
1288 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
1289 {
1290 /*      printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
1291         ReleaseSemaphore(ovl->hEvent, 1, NULL);
1292         completion_count++;
1293 }
1294
1295 static void test_async_file_errors(void)
1296 {
1297     char szFile[MAX_PATH];
1298     HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
1299     HANDLE hFile;
1300     LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
1301     OVERLAPPED ovl;
1302     ovl.Offset = 0;
1303     ovl.OffsetHigh = 0;
1304     ovl.hEvent = hSem;
1305     completion_count = 0;
1306     szFile[0] = '\0';
1307     GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
1308     strcat(szFile, "\\win.ini");
1309     hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
1310     ok(hFile != NULL, "CreateFileA(%s ...) failed\n", szFile);
1311     while (TRUE)
1312     {
1313         BOOL res;
1314         while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
1315             ;
1316         res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
1317         /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
1318         if (!res)
1319             break;
1320         ovl.Offset += 4096;
1321         /* i/o completion routine only called if ReadFileEx returned success.
1322          * we only care about violations of this rule so undo what should have
1323          * been done */
1324         completion_count--;
1325     }
1326     ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
1327     /*printf("Error = %ld\n", GetLastError());*/
1328 }
1329
1330 static void test_read_write(void)
1331 {
1332     DWORD bytes, ret;
1333     HANDLE hFile;
1334     char temp_path[MAX_PATH];
1335     char filename[MAX_PATH];
1336     static const char prefix[] = "pfx";
1337
1338     ret = GetTempPathA(MAX_PATH, temp_path);
1339     ok(ret != 0, "GetTempPathA error %ld\n", GetLastError());
1340     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1341
1342     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
1343     ok(ret != 0, "GetTempFileNameA error %ld\n", GetLastError());
1344
1345     hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1346                         CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1347     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %ld\n", GetLastError());
1348
1349     SetLastError(12345678);
1350     bytes = 12345678;
1351     ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
1352     ok(ret && GetLastError() == 12345678,
1353         "ret = %ld, error %ld\n", ret, GetLastError());
1354     ok(!bytes, "bytes = %ld\n", bytes);
1355
1356     SetLastError(12345678);
1357     bytes = 12345678;
1358     ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
1359     ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
1360         (ret && GetLastError() == 12345678), /* Win9x */
1361         "ret = %ld, error %ld\n", ret, GetLastError());
1362     ok(!bytes || /* Win2k */
1363         bytes == 10, /* Win9x */
1364         "bytes = %ld\n", bytes);
1365
1366     SetLastError(12345678);
1367     bytes = 12345678;
1368     ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
1369     ok(ret && GetLastError() == 12345678,
1370         "ret = %ld, error %ld\n", ret, GetLastError());
1371     ok(!bytes, "bytes = %ld\n", bytes);
1372
1373     SetLastError(12345678);
1374     bytes = 12345678;
1375     ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
1376     ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
1377                 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
1378         "ret = %ld, error %ld\n", ret, GetLastError());
1379     ok(!bytes, "bytes = %ld\n", bytes);
1380
1381     ret = CloseHandle(hFile);
1382     ok( ret, "CloseHandle: error %ld\n", GetLastError());
1383     ret = DeleteFileA(filename);
1384     ok( ret, "DeleteFileA: error %ld\n", GetLastError());
1385 }
1386
1387 START_TEST(file)
1388 {
1389     test__hread(  );
1390     test__hwrite(  );
1391     test__lclose(  );
1392     test__lcreat(  );
1393     test__llseek(  );
1394     test__llopen(  );
1395     test__lread(  );
1396     test__lwrite(  );
1397     test_GetTempFileNameA();
1398     test_CopyFileA();
1399     test_CopyFileW();
1400     test_CreateFileA();
1401     test_CreateFileW();
1402     test_DeleteFileA();
1403     test_DeleteFileW();
1404     test_MoveFileA();
1405     test_MoveFileW();
1406     test_FindFirstFileA();
1407     test_FindNextFileA();
1408     test_LockFile();
1409     test_file_sharing();
1410     test_offset_in_overlapped_structure();
1411     test_MapFile();
1412     test_GetFileType();
1413     test_async_file_errors();
1414     test_read_write();
1415 }