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