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