SHGetFileInfo should tolerate null pointers.
[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 <stdlib.h>
23 #include <time.h>
24
25 #include "wine/test.h"
26 #include "winbase.h"
27 #include "winerror.h"
28
29
30 LPCSTR filename = "testfile.xxx";
31 LPCSTR sillytext =
32 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
33 "1234 43 4kljf lf &%%%&&&&&& 34 4 34   3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf  \n  \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
42
43
44 static void test__hread( void )
45 {
46     HFILE filehandle;
47     char buffer[10000];
48     long bytes_read;
49     long bytes_wanted;
50     long i;
51
52     SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
53     DeleteFileA( filename );
54     filehandle = _lcreat( filename, 0 );
55     if (filehandle == HFILE_ERROR)
56     {
57         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
58         return;
59     }
60
61     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
62
63     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
64
65     filehandle = _lopen( filename, OF_READ );
66
67     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError(  ) );
68
69     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
70
71     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
72
73     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
74     {
75         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
76         ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
77         for (i = 0; i < bytes_wanted; i++)
78         {
79             ok( buffer[i] == sillytext[i], "that's not what's written" );
80         }
81     }
82
83     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
84
85     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
86 }
87
88
89 static void test__hwrite( void )
90 {
91     HFILE filehandle;
92     char buffer[10000];
93     long bytes_read;
94     long bytes_written;
95     long blocks;
96     long i;
97     char *contents;
98     HLOCAL memory_object;
99     char checksum[1];
100
101     filehandle = _lcreat( filename, 0 );
102     if (filehandle == HFILE_ERROR)
103     {
104         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
105         return;
106     }
107
108     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
109
110     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
111
112     filehandle = _lopen( filename, OF_READ );
113
114     bytes_read = _hread( filehandle, buffer, 1);
115
116     ok( 0 == bytes_read, "file read size error" );
117
118     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
119
120     filehandle = _lopen( filename, OF_READWRITE );
121
122     bytes_written = 0;
123     checksum[0] = '\0';
124     srand( (unsigned)time( NULL ) );
125     for (blocks = 0; blocks < 100; blocks++)
126     {
127         for (i = 0; i < sizeof( buffer ); i++)
128         {
129             buffer[i] = rand(  );
130             checksum[0] = checksum[0] + buffer[i];
131         }
132         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
133         bytes_written = bytes_written + sizeof( buffer );
134     }
135
136     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
137     bytes_written++;
138
139     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
140
141     memory_object = LocalAlloc( LPTR, bytes_written );
142
143     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
144
145     contents = LocalLock( memory_object );
146
147     filehandle = _lopen( filename, OF_READ );
148
149     contents = LocalLock( memory_object );
150
151     ok( NULL != contents, "LocalLock whines" );
152
153     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
154
155     checksum[0] = '\0';
156     i = 0;
157     do
158     {
159         checksum[0] = checksum[0] + contents[i];
160         i++;
161     }
162     while (i < bytes_written - 1);
163
164     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
165
166     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
167
168     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
169 }
170
171
172 static void test__lclose( void )
173 {
174     HFILE filehandle;
175
176     filehandle = _lcreat( filename, 0 );
177     if (filehandle == HFILE_ERROR)
178     {
179         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
180         return;
181     }
182
183     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
184
185     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
186
187     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
188 }
189
190
191 static void test__lcreat( void )
192 {
193     HFILE filehandle;
194     char buffer[10000];
195     WIN32_FIND_DATAA search_results;
196     char slashname[] = "testfi/";
197     HANDLE find;
198
199     filehandle = _lcreat( filename, 0 );
200     if (filehandle == HFILE_ERROR)
201     {
202         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
203         return;
204     }
205
206     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
207
208     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
209
210     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
211
212     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
213
214     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
215
216     ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
217
218     filehandle = _lcreat( filename, 1 ); /* readonly */
219     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
220
221     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
222
223     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
224
225     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
226
227     ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
228
229     ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
230
231     ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
232
233     filehandle = _lcreat( filename, 2 );
234     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
235
236     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
237
238     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
239
240     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
241
242     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
243
244     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
245
246     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
247
248     filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
249     ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError(  ) );
250
251     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
252
253     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
254
255     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  lstrlenA( sillytext ), "erratic _hread return value" );
256
257     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
258
259     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
260
261     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
262
263     filehandle=_lcreat (slashname, 0); /* illegal name */
264     if (HFILE_ERROR==filehandle)
265       ok (0, "couldn't create file \"%s\" (err=%ld)", slashname,
266           GetLastError ());
267     else {
268       _lclose(filehandle);
269       find=FindFirstFileA (slashname, &search_results);
270       if (INVALID_HANDLE_VALUE==find)
271         ok (0, "file \"%s\" not found", slashname);
272       else {
273         ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
274         slashname[strlen(slashname)-1]=0;
275         ok (!strcmp (slashname, search_results.cFileName),
276             "found unexpected name \"%s\"", search_results.cFileName);
277         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
278             "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
279             search_results.dwFileAttributes);
280       }
281       ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
282           GetLastError ());
283     }
284
285     filehandle=_lcreat (filename, 8); /* illegal attribute */
286     if (HFILE_ERROR==filehandle)
287       ok (0, "couldn't create volume label \"%s\"", filename);
288     else {
289       _lclose(filehandle);
290       find=FindFirstFileA (filename, &search_results);
291       if (INVALID_HANDLE_VALUE==find)
292         ok (0, "file \"%s\" not found", filename);
293       else {
294         ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
295         ok (!strcmp (filename, search_results.cFileName),
296             "found unexpected name \"%s\"", search_results.cFileName);
297         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
298             "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
299             search_results.dwFileAttributes);
300       }
301       ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
302           GetLastError ());
303     }
304 }
305
306
307 void test__llseek( void )
308 {
309     INT i;
310     HFILE filehandle;
311     char buffer[1];
312     long bytes_read;
313
314     filehandle = _lcreat( filename, 0 );
315     if (filehandle == HFILE_ERROR)
316     {
317         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
318         return;
319     }
320
321     for (i = 0; i < 400; i++)
322     {
323         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
324     }
325     ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
326     ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
327
328     bytes_read = _hread( filehandle, buffer, 1);
329     ok( 1 == bytes_read, "file read size error" );
330     ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
331     ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
332
333     bytes_read = _hread( filehandle, buffer, 1);
334     ok( 1 == bytes_read, "file read size error" );
335     ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
336     ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
337     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
338
339     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
340 }
341
342
343 static void test__llopen( void )
344 {
345     HFILE filehandle;
346     UINT bytes_read;
347     char buffer[10000];
348
349     filehandle = _lcreat( filename, 0 );
350     if (filehandle == HFILE_ERROR)
351     {
352         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
353         return;
354     }
355
356     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
357     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
358
359     filehandle = _lopen( filename, OF_READ );
360     ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
361     bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
362     ok( strlen( sillytext )  == bytes_read, "file read size error" );
363     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
364
365     filehandle = _lopen( filename, OF_READWRITE );
366     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
367     ok( strlen( sillytext )  == bytes_read, "file read size error" );
368     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
369     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
370
371     filehandle = _lopen( filename, OF_WRITE );
372     ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
373     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
374     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
375
376     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
377     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
378 }
379
380
381 static void test__lread( void )
382 {
383     HFILE filehandle;
384     char buffer[10000];
385     long bytes_read;
386     UINT bytes_wanted;
387     UINT i;
388
389     filehandle = _lcreat( filename, 0 );
390     if (filehandle == HFILE_ERROR)
391     {
392         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
393         return;
394     }
395
396     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
397
398     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
399
400     filehandle = _lopen( filename, OF_READ );
401
402     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
403
404     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
405
406     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
407
408     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
409     {
410         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
411         ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
412         for (i = 0; i < bytes_wanted; i++)
413         {
414             ok( buffer[i] == sillytext[i], "that's not what's written" );
415         }
416     }
417
418     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
419
420     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
421 }
422
423
424 static void test__lwrite( void )
425 {
426     HFILE filehandle;
427     char buffer[10000];
428     long bytes_read;
429     long bytes_written;
430     long blocks;
431     long i;
432     char *contents;
433     HLOCAL memory_object;
434     char checksum[1];
435
436     filehandle = _lcreat( filename, 0 );
437     if (filehandle == HFILE_ERROR)
438     {
439         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
440         return;
441     }
442
443     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
444
445     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
446
447     filehandle = _lopen( filename, OF_READ );
448
449     bytes_read = _hread( filehandle, buffer, 1);
450
451     ok( 0 == bytes_read, "file read size error" );
452
453     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
454
455     filehandle = _lopen( filename, OF_READWRITE );
456
457     bytes_written = 0;
458     checksum[0] = '\0';
459     srand( (unsigned)time( NULL ) );
460     for (blocks = 0; blocks < 100; blocks++)
461     {
462         for (i = 0; i < sizeof( buffer ); i++)
463         {
464             buffer[i] = rand(  );
465             checksum[0] = checksum[0] + buffer[i];
466         }
467         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
468         bytes_written = bytes_written + sizeof( buffer );
469     }
470
471     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
472     bytes_written++;
473
474     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
475
476     memory_object = LocalAlloc( LPTR, bytes_written );
477
478     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
479
480     contents = LocalLock( memory_object );
481
482     filehandle = _lopen( filename, OF_READ );
483
484     contents = LocalLock( memory_object );
485
486     ok( NULL != contents, "LocalLock whines" );
487
488     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
489
490     checksum[0] = '\0';
491     i = 0;
492     do
493     {
494         checksum[0] += contents[i];
495         i++;
496     }
497     while (i < bytes_written - 1);
498
499     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
500
501     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
502
503     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
504 }
505
506 void test_CopyFileA(void)
507 {
508     char temp_path[MAX_PATH];
509     char source[MAX_PATH], dest[MAX_PATH];
510     static const char prefix[] = "pfx";
511     DWORD ret;
512
513     ret = GetTempPathA(MAX_PATH, temp_path);
514     ok(ret != 0, "GetTempPathA error %ld", GetLastError());
515     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
516
517     ret = GetTempFileNameA(temp_path, prefix, 0, source);
518     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
519
520     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
521     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
522
523     ret = CopyFileA(source, dest, TRUE);
524     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
525        "CopyFileA: unexpected error %ld\n", GetLastError());
526
527     ret = CopyFileA(source, dest, FALSE);
528     ok(ret,  "CopyFileA: error %ld\n", GetLastError());
529
530     ret = DeleteFileA(source);
531     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
532     ret = DeleteFileA(dest);
533     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
534 }
535
536 void test_CopyFileW(void)
537 {
538     WCHAR temp_path[MAX_PATH];
539     WCHAR source[MAX_PATH], dest[MAX_PATH];
540     static const WCHAR prefix[] = {'p','f','x',0};
541     DWORD ret;
542
543     ret = GetTempPathW(MAX_PATH, temp_path);
544     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
545         return;
546     ok(ret != 0, "GetTempPathW error %ld", GetLastError());
547     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
548
549     ret = GetTempFileNameW(temp_path, prefix, 0, source);
550     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
551
552     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
553     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
554
555     ret = CopyFileW(source, dest, TRUE);
556     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
557        "CopyFileW: unexpected error %ld\n", GetLastError());
558
559     ret = CopyFileW(source, dest, FALSE);
560     ok(ret,  "CopyFileW: error %ld\n", GetLastError());
561
562     ret = DeleteFileW(source);
563     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
564     ret = DeleteFileW(dest);
565     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
566 }
567
568 void test_CreateFileA(void)
569 {
570     HANDLE hFile;
571     char temp_path[MAX_PATH];
572     char filename[MAX_PATH];
573     static const char prefix[] = "pfx";
574     DWORD ret;
575
576     ret = GetTempPathA(MAX_PATH, temp_path);
577     ok(ret != 0, "GetTempPathA error %ld", GetLastError());
578     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
579
580     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
581     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
582
583     hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
584                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
585     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
586         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
587
588     ret = DeleteFileA(filename);
589     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
590 }
591
592 void test_CreateFileW(void)
593 {
594     HANDLE hFile;
595     WCHAR temp_path[MAX_PATH];
596     WCHAR filename[MAX_PATH];
597     static const WCHAR prefix[] = {'p','f','x',0};
598     DWORD ret;
599
600     ret = GetTempPathW(MAX_PATH, temp_path);
601     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
602         return;
603     ok(ret != 0, "GetTempPathW error %ld", GetLastError());
604     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
605
606     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
607     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
608
609     hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
610                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
611     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
612         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
613
614     ret = DeleteFileW(filename);
615     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
616 }
617
618 static void test_DeleteFileA( void )
619 {
620     BOOL ret;
621
622     ret = DeleteFileA(NULL);
623     ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
624                 GetLastError() == ERROR_PATH_NOT_FOUND),
625        "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
626
627     ret = DeleteFileA("");
628     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
629                 GetLastError() == ERROR_BAD_PATHNAME),
630        "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
631 }
632
633 static void test_DeleteFileW( void )
634 {
635     BOOL ret;
636     WCHAR emptyW[]={'\0'};
637
638     ret = DeleteFileW(NULL);
639     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
640         return;
641     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
642        "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
643
644     ret = DeleteFileW(emptyW);
645     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
646        "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
647 }
648
649 #define PATTERN_OFFSET 0x10
650
651 void test_offset_in_overlapped_structure(void)
652 {
653     HANDLE hFile;
654     OVERLAPPED ov;
655     DWORD done;
656     BOOL rc;
657     BYTE buf[256], pattern[] = "TeSt";
658     UINT i;
659     char temp_path[MAX_PATH], temp_fname[MAX_PATH];
660
661     ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
662     ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
663
664     /*** Write File *****************************************************/
665
666     hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
667     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
668
669     for(i = 0; i < sizeof(buf); i++) buf[i] = i;
670     ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
671     ok(done == sizeof(buf), "expected number of bytes written %lu", done);
672
673     memset(&ov, 0, sizeof(ov));
674     ov.Offset = PATTERN_OFFSET;
675     ov.OffsetHigh = 0;
676     rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
677     /* Win 9x does not support the overlapped I/O on files */
678     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
679         ok(rc, "WriteFile error %ld", GetLastError());
680         ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
681         trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
682         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
683            "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
684
685         ov.Offset = sizeof(buf) * 2;
686         ov.OffsetHigh = 0;
687         ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
688         ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
689         /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
690         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
691            "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
692     }
693
694     CloseHandle(hFile);
695
696     /*** Read File *****************************************************/
697
698     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
699     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
700
701     memset(buf, 0, sizeof(buf));
702     memset(&ov, 0, sizeof(ov));
703     ov.Offset = PATTERN_OFFSET;
704     ov.OffsetHigh = 0;
705     rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
706     /* Win 9x does not support the overlapped I/O on files */
707     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
708         ok(rc, "ReadFile error %ld", GetLastError());
709         ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
710         trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
711         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
712            "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
713         ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
714     }
715
716     CloseHandle(hFile);
717
718     ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
719 }
720
721 static void test_LockFile(void)
722 {
723     HANDLE handle;
724     DWORD written;
725     OVERLAPPED overlapped;
726
727     handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
728                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
729                           CREATE_ALWAYS, 0, 0 );
730     if (handle == INVALID_HANDLE_VALUE)
731     {
732         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
733         return;
734     }
735     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
736
737     ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
738     ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
739     ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
740
741     ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
742     /* overlapping locks must fail */
743     ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
744     ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
745     /* non-overlapping locks must succeed */
746     ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
747
748     ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
749     ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
750     ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
751     ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
752
753     overlapped.Offset = 100;
754     overlapped.OffsetHigh = 0;
755     overlapped.hEvent = 0;
756     ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
757     /* overlapping shared locks are OK */
758     overlapped.Offset = 150;
759     ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
760     /* but exclusive is not */
761     ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
762         "LockFileEx exclusive 150,50 succeeded" );
763     ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
764     ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
765     overlapped.Offset = 100;
766     ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
767     ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
768
769     ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
770     ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
771     ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
772     ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
773
774     /* wrap-around lock should not do anything */
775     /* (but still succeeds on NT4 so we don't check result) */
776     LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
777     ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
778     ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
779
780     /* zero-byte lock */
781     ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
782     ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
783     ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
784     ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
785     ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
786     ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
787     ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
788
789     CloseHandle( handle );
790     DeleteFileA( filename );
791 }
792
793 void test_FindFirstFileA()
794 {
795     HANDLE handle;
796     WIN32_FIND_DATAA search_results;
797     int err;
798
799     handle = FindFirstFileA("C:",&search_results);
800     err = GetLastError();
801     ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
802     if (handle == INVALID_HANDLE_VALUE)
803       ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
804     handle = FindFirstFileA("C:\\",&search_results);
805     err = GetLastError();
806     ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
807     if (handle == INVALID_HANDLE_VALUE)
808       ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number\n");
809     handle = FindFirstFileA("C:\\*",&search_results);
810     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
811     ok ( FindClose(handle) == TRUE, "Failed to close handle");
812 }
813
814 START_TEST(file)
815 {
816     test__hread(  );
817     test__hwrite(  );
818     test__lclose(  );
819     test__lcreat(  );
820     test__llseek(  );
821     test__llopen(  );
822     test__lread(  );
823     test__lwrite(  );
824     test_CopyFileA();
825     test_CopyFileW();
826     test_CreateFileA();
827     test_CreateFileW();
828     test_DeleteFileA();
829     test_DeleteFileW();
830     test_FindFirstFileA();
831     test_LockFile();
832     test_offset_in_overlapped_structure();
833 }