Added tests for FoldStringA/W.
[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)",filename,GetLastError());
59         return;
60     }
61
62     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
63
64     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
65
66     filehandle = _lopen( filename, OF_READ );
67
68     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError(  ) );
69
70     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
71
72     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
73
74     for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
75     {
76         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
77         ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
78         for (i = 0; i < bytes_wanted; i++)
79         {
80             ok( buffer[i] == sillytext[i], "that's not what's written" );
81         }
82     }
83
84     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
85
86     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", 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)",filename,GetLastError());
106         return;
107     }
108
109     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
110
111     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
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" );
118
119     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
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" );
134         bytes_written = bytes_written + sizeof( buffer );
135     }
136
137     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
138     bytes_written++;
139
140     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
141
142     memory_object = LocalAlloc( LPTR, bytes_written );
143
144     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
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" );
153
154     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
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" );
166
167     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
168
169     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", 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)",filename,GetLastError());
181         return;
182     }
183
184     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
185
186     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
187
188     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", 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)",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       err=GetLastError ();
268       ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
269           "creating file \"%s\" failed with error %d", slashname, err);
270     } else { /* only NT succeeds */
271       _lclose(filehandle);
272       find=FindFirstFileA (slashname, &search_results);
273       if (INVALID_HANDLE_VALUE==find)
274         ok (0, "file \"%s\" not found", slashname);
275       else {
276         ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
277         slashname[strlen(slashname)-1]=0;
278         ok (!strcmp (slashname, search_results.cFileName),
279             "found unexpected name \"%s\"", search_results.cFileName);
280         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
281             "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
282             search_results.dwFileAttributes);
283       }
284       ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
285           GetLastError ());
286     }
287
288     filehandle=_lcreat (filename, 8); /* illegal attribute */
289     if (HFILE_ERROR==filehandle)
290       ok (0, "couldn't create volume label \"%s\"", filename);
291     else {
292       _lclose(filehandle);
293       find=FindFirstFileA (filename, &search_results);
294       if (INVALID_HANDLE_VALUE==find)
295         ok (0, "file \"%s\" not found", filename);
296       else {
297         ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
298         ok (!strcmp (filename, search_results.cFileName),
299             "found unexpected name \"%s\"", search_results.cFileName);
300         ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301             "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
302             search_results.dwFileAttributes);
303       }
304       ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
305           GetLastError ());
306     }
307 }
308
309
310 void test__llseek( void )
311 {
312     INT i;
313     HFILE filehandle;
314     char buffer[1];
315     long bytes_read;
316
317     filehandle = _lcreat( filename, 0 );
318     if (filehandle == HFILE_ERROR)
319     {
320         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
321         return;
322     }
323
324     for (i = 0; i < 400; i++)
325     {
326         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
327     }
328     ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
329     ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
330
331     bytes_read = _hread( filehandle, buffer, 1);
332     ok( 1 == bytes_read, "file read size error" );
333     ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
334     ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
335
336     bytes_read = _hread( filehandle, buffer, 1);
337     ok( 1 == bytes_read, "file read size error" );
338     ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
339     ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
340     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
341
342     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
343 }
344
345
346 static void test__llopen( void )
347 {
348     HFILE filehandle;
349     UINT bytes_read;
350     char buffer[10000];
351
352     filehandle = _lcreat( filename, 0 );
353     if (filehandle == HFILE_ERROR)
354     {
355         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
356         return;
357     }
358
359     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
360     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
361
362     filehandle = _lopen( filename, OF_READ );
363     ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
364     bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
365     ok( strlen( sillytext )  == bytes_read, "file read size error" );
366     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
367
368     filehandle = _lopen( filename, OF_READWRITE );
369     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
370     ok( strlen( sillytext )  == bytes_read, "file read size error" );
371     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
372     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
373
374     filehandle = _lopen( filename, OF_WRITE );
375     ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
376     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
377     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
378
379     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
380     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
381 }
382
383
384 static void test__lread( void )
385 {
386     HFILE filehandle;
387     char buffer[10000];
388     long bytes_read;
389     UINT bytes_wanted;
390     UINT i;
391
392     filehandle = _lcreat( filename, 0 );
393     if (filehandle == HFILE_ERROR)
394     {
395         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
396         return;
397     }
398
399     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
400
401     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
402
403     filehandle = _lopen( filename, OF_READ );
404
405     ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
406
407     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
408
409     ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
410
411     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
412     {
413         ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
414         ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
415         for (i = 0; i < bytes_wanted; i++)
416         {
417             ok( buffer[i] == sillytext[i], "that's not what's written" );
418         }
419     }
420
421     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
422
423     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
424 }
425
426
427 static void test__lwrite( void )
428 {
429     HFILE filehandle;
430     char buffer[10000];
431     long bytes_read;
432     long bytes_written;
433     long blocks;
434     long i;
435     char *contents;
436     HLOCAL memory_object;
437     char checksum[1];
438
439     filehandle = _lcreat( filename, 0 );
440     if (filehandle == HFILE_ERROR)
441     {
442         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
443         return;
444     }
445
446     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
447
448     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
449
450     filehandle = _lopen( filename, OF_READ );
451
452     bytes_read = _hread( filehandle, buffer, 1);
453
454     ok( 0 == bytes_read, "file read size error" );
455
456     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
457
458     filehandle = _lopen( filename, OF_READWRITE );
459
460     bytes_written = 0;
461     checksum[0] = '\0';
462     srand( (unsigned)time( NULL ) );
463     for (blocks = 0; blocks < 100; blocks++)
464     {
465         for (i = 0; i < (long)sizeof( buffer ); i++)
466         {
467             buffer[i] = rand(  );
468             checksum[0] = checksum[0] + buffer[i];
469         }
470         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
471         bytes_written = bytes_written + sizeof( buffer );
472     }
473
474     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
475     bytes_written++;
476
477     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
478
479     memory_object = LocalAlloc( LPTR, bytes_written );
480
481     ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
482
483     contents = LocalLock( memory_object );
484
485     filehandle = _lopen( filename, OF_READ );
486
487     contents = LocalLock( memory_object );
488
489     ok( NULL != contents, "LocalLock whines" );
490
491     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
492
493     checksum[0] = '\0';
494     i = 0;
495     do
496     {
497         checksum[0] += contents[i];
498         i++;
499     }
500     while (i < bytes_written - 1);
501
502     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
503
504     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
505
506     ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError(  ) );
507 }
508
509 void test_CopyFileA(void)
510 {
511     char temp_path[MAX_PATH];
512     char source[MAX_PATH], dest[MAX_PATH];
513     static const char prefix[] = "pfx";
514     DWORD ret;
515
516     ret = GetTempPathA(MAX_PATH, temp_path);
517     ok(ret != 0, "GetTempPathA error %ld", GetLastError());
518     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
519
520     ret = GetTempFileNameA(temp_path, prefix, 0, source);
521     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
522
523     ret = GetTempFileNameA(temp_path, prefix, 0, dest);
524     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
525
526     ret = CopyFileA(source, dest, TRUE);
527     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
528        "CopyFileA: unexpected error %ld\n", GetLastError());
529
530     ret = CopyFileA(source, dest, FALSE);
531     ok(ret,  "CopyFileA: error %ld\n", GetLastError());
532
533     ret = DeleteFileA(source);
534     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
535     ret = DeleteFileA(dest);
536     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
537 }
538
539 void test_CopyFileW(void)
540 {
541     WCHAR temp_path[MAX_PATH];
542     WCHAR source[MAX_PATH], dest[MAX_PATH];
543     static const WCHAR prefix[] = {'p','f','x',0};
544     DWORD ret;
545
546     ret = GetTempPathW(MAX_PATH, temp_path);
547     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
548         return;
549     ok(ret != 0, "GetTempPathW error %ld", GetLastError());
550     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
551
552     ret = GetTempFileNameW(temp_path, prefix, 0, source);
553     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
554
555     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
556     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
557
558     ret = CopyFileW(source, dest, TRUE);
559     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
560        "CopyFileW: unexpected error %ld\n", GetLastError());
561
562     ret = CopyFileW(source, dest, FALSE);
563     ok(ret,  "CopyFileW: error %ld\n", GetLastError());
564
565     ret = DeleteFileW(source);
566     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
567     ret = DeleteFileW(dest);
568     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
569 }
570
571 void test_CreateFileA(void)
572 {
573     HANDLE hFile;
574     char temp_path[MAX_PATH];
575     char filename[MAX_PATH];
576     static const char prefix[] = "pfx";
577     DWORD ret;
578
579     ret = GetTempPathA(MAX_PATH, temp_path);
580     ok(ret != 0, "GetTempPathA error %ld", GetLastError());
581     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
582
583     ret = GetTempFileNameA(temp_path, prefix, 0, filename);
584     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
585
586     hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
587                         CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
588     ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
589         "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
590
591     ret = DeleteFileA(filename);
592     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
593 }
594
595 void test_CreateFileW(void)
596 {
597     HANDLE hFile;
598     WCHAR temp_path[MAX_PATH];
599     WCHAR filename[MAX_PATH];
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", GetLastError());
607     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
608
609     ret = GetTempFileNameW(temp_path, prefix, 0, filename);
610     ok(ret != 0, "GetTempFileNameW error %ld", 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");
616
617     ret = DeleteFileW(filename);
618     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
619 }
620
621 static void test_DeleteFileA( void )
622 {
623     BOOL ret;
624
625     ret = DeleteFileA(NULL);
626     ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
627                 GetLastError() == ERROR_PATH_NOT_FOUND),
628        "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
629
630     ret = DeleteFileA("");
631     ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
632                 GetLastError() == ERROR_BAD_PATHNAME),
633        "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
634 }
635
636 static void test_DeleteFileW( void )
637 {
638     BOOL ret;
639     WCHAR emptyW[]={'\0'};
640
641     ret = DeleteFileW(NULL);
642     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
643         return;
644     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
645        "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
646
647     ret = DeleteFileW(emptyW);
648     ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
649        "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
650 }
651
652 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
653
654 void test_MoveFileA(void)
655 {
656     char tempdir[MAX_PATH];
657     char source[MAX_PATH], dest[MAX_PATH];
658     static const char prefix[] = "pfx";
659     DWORD ret;
660
661     ret = GetTempPathA(MAX_PATH, tempdir);
662     ok(ret != 0, "GetTempPathA error %ld", GetLastError());
663     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
664
665     ret = GetTempFileNameA(tempdir, prefix, 0, source);
666     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
667
668     ret = GetTempFileNameA(tempdir, prefix, 0, dest);
669     ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
670
671     ret = MoveFileA(source, dest);
672     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
673        "MoveFileA: unexpected error %ld\n", GetLastError());
674
675     ret = DeleteFileA(dest);
676     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
677
678     ret = MoveFileA(source, dest);
679     ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
680
681     lstrcatA(tempdir, "Remove Me");
682     ret = CreateDirectoryA(tempdir, NULL);
683     ok(ret == TRUE, "CreateDirectoryA failed");
684
685     lstrcpyA(source, dest);
686     lstrcpyA(dest, tempdir);
687     lstrcatA(dest, "\\wild?.*");
688     ret = MoveFileA(source, dest);
689     todo_wine {
690           ok(!ret, "MoveFileA: shouldn't move to wildcard file");
691       ok(GetLastError() == ERROR_INVALID_NAME,
692               "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
693       if (ret || (GetLastError() != ERROR_INVALID_NAME))
694           {
695         WIN32_FIND_DATAA fd;
696         char temppath[MAX_PATH];
697         HANDLE hFind;
698
699         lstrcpyA(temppath, tempdir);
700         lstrcatA(temppath, "\\*.*");
701         hFind = FindFirstFileA(temppath, &fd);
702         if (INVALID_HANDLE_VALUE != hFind)
703                 {
704           LPSTR lpName;
705           do
706                   {
707             lpName = fd.cAlternateFileName;
708             if (!lpName[0])
709               lpName = fd.cFileName;
710             ok(!IsDotDir(lpName), "MoveFileA: wildcards file created!");
711                   }
712           while (FindNextFileA(hFind, &fd));
713           FindClose(hFind);
714                 }
715           }
716     }
717
718     ret = DeleteFileA(source);
719     ok(ret, "DeleteFileA: error %ld\n", GetLastError());
720     ret = DeleteFileA(dest);
721     ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
722     ret = RemoveDirectoryA(tempdir);
723     ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
724 }
725
726 void test_MoveFileW(void)
727 {
728     WCHAR temp_path[MAX_PATH];
729     WCHAR source[MAX_PATH], dest[MAX_PATH];
730     static const WCHAR prefix[] = {'p','f','x',0};
731     DWORD ret;
732
733     ret = GetTempPathW(MAX_PATH, temp_path);
734     if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
735         return;
736     ok(ret != 0, "GetTempPathW error %ld", GetLastError());
737     ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
738
739     ret = GetTempFileNameW(temp_path, prefix, 0, source);
740     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
741
742     ret = GetTempFileNameW(temp_path, prefix, 0, dest);
743     ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
744
745     ret = MoveFileW(source, dest);
746     ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
747        "CopyFileW: unexpected error %ld\n", GetLastError());
748
749     ret = DeleteFileW(source);
750     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
751     ret = DeleteFileW(dest);
752     ok(ret, "DeleteFileW: error %ld\n", GetLastError());
753 }
754
755 #define PATTERN_OFFSET 0x10
756
757 void test_offset_in_overlapped_structure(void)
758 {
759     HANDLE hFile;
760     OVERLAPPED ov;
761     DWORD done;
762     BOOL rc;
763     BYTE buf[256], pattern[] = "TeSt";
764     UINT i;
765     char temp_path[MAX_PATH], temp_fname[MAX_PATH];
766
767     ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
768     ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
769
770     /*** Write File *****************************************************/
771
772     hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
773     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
774
775     for(i = 0; i < sizeof(buf); i++) buf[i] = i;
776     ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
777     ok(done == sizeof(buf), "expected number of bytes written %lu", done);
778
779     memset(&ov, 0, sizeof(ov));
780     ov.Offset = PATTERN_OFFSET;
781     ov.OffsetHigh = 0;
782     rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
783     /* Win 9x does not support the overlapped I/O on files */
784     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
785         ok(rc, "WriteFile error %ld", GetLastError());
786         ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
787         trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
788         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
789            "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
790
791         ov.Offset = sizeof(buf) * 2;
792         ov.OffsetHigh = 0;
793         ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
794         ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
795         /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
796         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
797            "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
798     }
799
800     CloseHandle(hFile);
801
802     /*** Read File *****************************************************/
803
804     hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
805     ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
806
807     memset(buf, 0, sizeof(buf));
808     memset(&ov, 0, sizeof(ov));
809     ov.Offset = PATTERN_OFFSET;
810     ov.OffsetHigh = 0;
811     rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
812     /* Win 9x does not support the overlapped I/O on files */
813     if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
814         ok(rc, "ReadFile error %ld", GetLastError());
815         ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
816         trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
817         ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
818            "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
819         ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
820     }
821
822     CloseHandle(hFile);
823
824     ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
825 }
826
827 static void test_LockFile(void)
828 {
829     HANDLE handle;
830     DWORD written;
831     OVERLAPPED overlapped;
832
833     handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
834                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
835                           CREATE_ALWAYS, 0, 0 );
836     if (handle == INVALID_HANDLE_VALUE)
837     {
838         ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
839         return;
840     }
841     ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
842
843     ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
844     ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
845     ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
846
847     ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
848     /* overlapping locks must fail */
849     ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
850     ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
851     /* non-overlapping locks must succeed */
852     ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
853
854     ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
855     ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
856     ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
857     ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
858
859     overlapped.Offset = 100;
860     overlapped.OffsetHigh = 0;
861     overlapped.hEvent = 0;
862     ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
863     /* overlapping shared locks are OK */
864     overlapped.Offset = 150;
865     ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
866     /* but exclusive is not */
867     ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
868         "LockFileEx exclusive 150,50 succeeded" );
869     ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
870     ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
871     overlapped.Offset = 100;
872     ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
873     ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
874
875     ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
876     ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
877     ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
878     ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
879
880     /* wrap-around lock should not do anything */
881     /* (but still succeeds on NT4 so we don't check result) */
882     LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
883     ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
884     ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
885
886     /* zero-byte lock */
887     ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
888     ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
889     ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
890     ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
891     ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
892     ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
893     ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
894
895     CloseHandle( handle );
896     DeleteFileA( filename );
897 }
898
899 void test_FindFirstFileA()
900 {
901     HANDLE handle;
902     WIN32_FIND_DATAA search_results;
903     int err;
904
905     handle = FindFirstFileA("C:\\",&search_results);
906     err = GetLastError();
907     ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
908     if (handle == INVALID_HANDLE_VALUE)
909       ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
910     handle = FindFirstFileA("C:\\*",&search_results);
911     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
912     ok ( FindClose(handle) == TRUE, "Failed to close handle");
913 }
914
915 void test_FindNextFileA()
916 {
917     HANDLE handle;
918     WIN32_FIND_DATAA search_results;
919     int err;
920
921     handle = FindFirstFileA("C:\\*",&search_results);
922     ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
923     while (FindNextFile(handle, &search_results))
924     {
925         /* get to the end of the files */
926     }
927     ok ( FindClose(handle) == TRUE, "Failed to close handle");
928     err = GetLastError();
929     ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES");
930 }
931
932 START_TEST(file)
933 {
934     test__hread(  );
935     test__hwrite(  );
936     test__lclose(  );
937     test__lcreat(  );
938     test__llseek(  );
939     test__llopen(  );
940     test__lread(  );
941     test__lwrite(  );
942     test_CopyFileA();
943     test_CopyFileW();
944     test_CreateFileA();
945     test_CreateFileW();
946     test_DeleteFileA();
947     test_DeleteFileW();
948     test_FindFirstFileA();
949     test_FindNextFileA();
950     test_LockFile();
951     test_offset_in_overlapped_structure();
952 }