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