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