[KERNEL32_WINETEST]
[reactos.git] / rostests / winetests / kernel32 / file.c
1 /*
2 * Unit tests for file functions in Wine
3 *
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 * Copyright (c) 2008 Jeff Zaroyko
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 */
22
23 /* ReplaceFile requires Windows 2000 or newer */
24 #define _WIN32_WINNT 0x0500
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <stdio.h>
30
31 #include "wine/test.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35
36 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
37 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
38 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
39 static UINT (WINAPI *pGetSystemWindowsDirectoryA)(LPSTR, UINT);
40 static BOOL (WINAPI *pGetVolumeNameForVolumeMountPointA)(LPCSTR, LPSTR, DWORD);
41 static DWORD WINAPI (*pQueueUserAPC)(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
42
43 /* keep filename and filenameW the same */
44 static const char filename[] = "testfile.xxx";
45 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
46 static const char sillytext[] =
47 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
48 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
49 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
50 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
51 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
52 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
53 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
54 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
55 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
56 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
57
58 struct test_list {
59 const char *file; /* file string to test */
60 const DWORD err; /* Win NT and further error code */
61 const LONG err2; /* Win 9x & ME error code or -1 */
62 const DWORD options; /* option flag to use for open */
63 const BOOL todo_flag; /* todo_wine indicator */
64 } ;
65
66 static void InitFunctionPointers(void)
67 {
68 HMODULE hkernel32 = GetModuleHandleA("kernel32");
69
70 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
71 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
72 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
73 pGetSystemWindowsDirectoryA=(void*)GetProcAddress(hkernel32, "GetSystemWindowsDirectoryA");
74 pGetVolumeNameForVolumeMountPointA = (void *) GetProcAddress(hkernel32, "GetVolumeNameForVolumeMountPointA");
75 pQueueUserAPC = (void *) GetProcAddress(hkernel32, "QueueUserAPC");
76 }
77
78 static void test__hread( void )
79 {
80 HFILE filehandle;
81 char buffer[10000];
82 LONG bytes_read;
83 LONG bytes_wanted;
84 LONG i;
85 BOOL ret;
86
87 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
88 DeleteFileA( filename );
89 filehandle = _lcreat( filename, 0 );
90 if (filehandle == HFILE_ERROR)
91 {
92 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
93 return;
94 }
95
96 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
97
98 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
99
100 filehandle = _lopen( filename, OF_READ );
101
102 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
103
104 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
105
106 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
107
108 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
109 {
110 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
111 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
112 for (i = 0; i < bytes_wanted; i++)
113 {
114 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
115 }
116 }
117
118 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
119
120 ret = DeleteFileA( filename );
121 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
122 }
123
124
125 static void test__hwrite( void )
126 {
127 HFILE filehandle;
128 char buffer[10000];
129 LONG bytes_read;
130 LONG bytes_written;
131 ULONG blocks;
132 LONG i;
133 char *contents;
134 HLOCAL memory_object;
135 char checksum[1];
136 BOOL ret;
137
138 filehandle = _lcreat( filename, 0 );
139 if (filehandle == HFILE_ERROR)
140 {
141 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
142 return;
143 }
144
145 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
146
147 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
148
149 filehandle = _lopen( filename, OF_READ );
150
151 bytes_read = _hread( filehandle, buffer, 1);
152
153 ok( 0 == bytes_read, "file read size error\n" );
154
155 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
156
157 filehandle = _lopen( filename, OF_READWRITE );
158
159 bytes_written = 0;
160 checksum[0] = '\0';
161 srand( (unsigned)time( NULL ) );
162 for (blocks = 0; blocks < 100; blocks++)
163 {
164 for (i = 0; i < (LONG)sizeof( buffer ); i++)
165 {
166 buffer[i] = rand( );
167 checksum[0] = checksum[0] + buffer[i];
168 }
169 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
170 bytes_written = bytes_written + sizeof( buffer );
171 }
172
173 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
174 bytes_written++;
175
176 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
177
178 memory_object = LocalAlloc( LPTR, bytes_written );
179
180 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
181
182 contents = LocalLock( memory_object );
183 ok( NULL != contents, "LocalLock whines\n" );
184
185 filehandle = _lopen( filename, OF_READ );
186
187 contents = LocalLock( memory_object );
188 ok( NULL != contents, "LocalLock whines\n" );
189
190 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
191
192 checksum[0] = '\0';
193 i = 0;
194 do
195 {
196 checksum[0] = checksum[0] + contents[i];
197 i++;
198 }
199 while (i < bytes_written - 1);
200
201 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
202
203 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
204
205 ret = DeleteFileA( filename );
206 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
207
208 LocalFree( contents );
209 }
210
211
212 static void test__lclose( void )
213 {
214 HFILE filehandle;
215 BOOL ret;
216
217 filehandle = _lcreat( filename, 0 );
218 if (filehandle == HFILE_ERROR)
219 {
220 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
221 return;
222 }
223
224 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
225
226 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
227
228 ret = DeleteFileA( filename );
229 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
230 }
231
232
233 static void test__lcreat( void )
234 {
235 HFILE filehandle;
236 char buffer[10000];
237 WIN32_FIND_DATAA search_results;
238 char slashname[] = "testfi/";
239 int err;
240 HANDLE find;
241 BOOL ret;
242
243 filehandle = _lcreat( filename, 0 );
244 if (filehandle == HFILE_ERROR)
245 {
246 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
247 return;
248 }
249
250 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
251
252 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
253
254 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
255
256 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
257
258 find = FindFirstFileA( filename, &search_results );
259 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
260 FindClose( find );
261
262 ret = DeleteFileA(filename);
263 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
264
265 filehandle = _lcreat( filename, 1 ); /* readonly */
266 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
267
268 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
269
270 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
271
272 find = FindFirstFileA( filename, &search_results );
273 ok( INVALID_HANDLE_VALUE != find, "should be able to find file\n" );
274 FindClose( find );
275
276 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
277
278 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
279
280 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
281
282 filehandle = _lcreat( filename, 2 );
283 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
284
285 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
286
287 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
288
289 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
290
291 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
292
293 find = FindFirstFileA( filename, &search_results );
294 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
295 FindClose( find );
296
297 ret = DeleteFileA( filename );
298 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
299
300 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
301 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
302
303 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
304
305 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
306
307 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
308
309 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
310
311 find = FindFirstFileA( filename, &search_results );
312 ok( INVALID_HANDLE_VALUE != find, "should STILL be able to find file\n" );
313 FindClose( find );
314
315 ret = DeleteFileA( filename );
316 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
317
318 filehandle=_lcreat (slashname, 0); /* illegal name */
319 if (HFILE_ERROR==filehandle) {
320 err=GetLastError ();
321 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
322 "creating file \"%s\" failed with error %d\n", slashname, err);
323 } else { /* only NT succeeds */
324 _lclose(filehandle);
325 find=FindFirstFileA (slashname, &search_results);
326 if (INVALID_HANDLE_VALUE!=find)
327 {
328 ret = FindClose (find);
329 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
330 slashname[strlen(slashname)-1]=0;
331 ok (!strcmp (slashname, search_results.cFileName),
332 "found unexpected name \"%s\"\n", search_results.cFileName);
333 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
334 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
335 search_results.dwFileAttributes);
336 }
337 ret = DeleteFileA( slashname );
338 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
339 }
340
341 filehandle=_lcreat (filename, 8); /* illegal attribute */
342 if (HFILE_ERROR==filehandle)
343 ok (0, "couldn't create volume label \"%s\"\n", filename);
344 else {
345 _lclose(filehandle);
346 find=FindFirstFileA (filename, &search_results);
347 if (INVALID_HANDLE_VALUE==find)
348 ok (0, "file \"%s\" not found\n", filename);
349 else {
350 ret = FindClose(find);
351 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
352 ok (!strcmp (filename, search_results.cFileName),
353 "found unexpected name \"%s\"\n", search_results.cFileName);
354 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
355 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
356 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
357 search_results.dwFileAttributes);
358 }
359 ret = DeleteFileA( filename );
360 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
361 }
362 }
363
364
365 static void test__llseek( void )
366 {
367 INT i;
368 HFILE filehandle;
369 char buffer[1];
370 LONG bytes_read;
371 BOOL ret;
372
373 filehandle = _lcreat( filename, 0 );
374 if (filehandle == HFILE_ERROR)
375 {
376 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
377 return;
378 }
379
380 for (i = 0; i < 400; i++)
381 {
382 ok( _hwrite( filehandle, sillytext, strlen( sillytext ) ) != -1, "_hwrite complains\n" );
383 }
384 ok( _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ) != -1, "should be able to seek\n" );
385 ok( _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ) != -1, "should be able to seek\n" );
386
387 bytes_read = _hread( filehandle, buffer, 1);
388 ok( 1 == bytes_read, "file read size error\n" );
389 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
390 ok( _llseek( filehandle, -400 * (LONG)strlen( sillytext ), FILE_END ) != -1, "should be able to seek\n" );
391
392 bytes_read = _hread( filehandle, buffer, 1);
393 ok( 1 == bytes_read, "file read size error\n" );
394 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
395 ok( _llseek( filehandle, 1000000, FILE_END ) != -1, "should be able to seek past file; poor, poor Windows programmers\n" );
396 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
397
398 ret = DeleteFileA( filename );
399 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
400 }
401
402
403 static void test__llopen( void )
404 {
405 HFILE filehandle;
406 UINT bytes_read;
407 char buffer[10000];
408 BOOL ret;
409
410 filehandle = _lcreat( filename, 0 );
411 if (filehandle == HFILE_ERROR)
412 {
413 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
414 return;
415 }
416
417 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
418 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
419
420 filehandle = _lopen( filename, OF_READ );
421 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
422 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
423 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
424 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
425
426 filehandle = _lopen( filename, OF_READWRITE );
427 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
428 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
429 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
430 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
431
432 filehandle = _lopen( filename, OF_WRITE );
433 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
434 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
435 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
436
437 ret = DeleteFileA( filename );
438 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
439 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
440 }
441
442
443 static void test__lread( void )
444 {
445 HFILE filehandle;
446 char buffer[10000];
447 UINT bytes_read;
448 UINT bytes_wanted;
449 UINT i;
450 BOOL ret;
451
452 filehandle = _lcreat( filename, 0 );
453 if (filehandle == HFILE_ERROR)
454 {
455 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
456 return;
457 }
458
459 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
460
461 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
462
463 filehandle = _lopen( filename, OF_READ );
464
465 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
466
467 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
468
469 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
470
471 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
472 {
473 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
474 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
475 for (i = 0; i < bytes_wanted; i++)
476 {
477 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
478 }
479 }
480
481 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
482
483 ret = DeleteFileA( filename );
484 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
485 }
486
487
488 static void test__lwrite( void )
489 {
490 HFILE filehandle;
491 char buffer[10000];
492 UINT bytes_read;
493 UINT bytes_written;
494 UINT blocks;
495 INT i;
496 char *contents;
497 HLOCAL memory_object;
498 char checksum[1];
499 BOOL ret;
500
501 filehandle = _lcreat( filename, 0 );
502 if (filehandle == HFILE_ERROR)
503 {
504 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
505 return;
506 }
507
508 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
509
510 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
511
512 filehandle = _lopen( filename, OF_READ );
513
514 bytes_read = _hread( filehandle, buffer, 1);
515
516 ok( 0 == bytes_read, "file read size error\n" );
517
518 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
519
520 filehandle = _lopen( filename, OF_READWRITE );
521
522 bytes_written = 0;
523 checksum[0] = '\0';
524 srand( (unsigned)time( NULL ) );
525 for (blocks = 0; blocks < 100; blocks++)
526 {
527 for (i = 0; i < (INT)sizeof( buffer ); i++)
528 {
529 buffer[i] = rand( );
530 checksum[0] = checksum[0] + buffer[i];
531 }
532 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
533 bytes_written = bytes_written + sizeof( buffer );
534 }
535
536 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
537 bytes_written++;
538
539 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
540
541 memory_object = LocalAlloc( LPTR, bytes_written );
542
543 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
544
545 contents = LocalLock( memory_object );
546 ok( NULL != contents, "LocalLock whines\n" );
547
548 filehandle = _lopen( filename, OF_READ );
549
550 contents = LocalLock( memory_object );
551 ok( NULL != contents, "LocalLock whines\n" );
552
553 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
554
555 checksum[0] = '\0';
556 i = 0;
557 do
558 {
559 checksum[0] += contents[i];
560 i++;
561 }
562 while (i < bytes_written - 1);
563
564 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
565
566 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
567
568 ret = DeleteFileA( filename );
569 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
570
571 LocalFree( contents );
572 }
573
574 static void test_CopyFileA(void)
575 {
576 char temp_path[MAX_PATH];
577 char source[MAX_PATH], dest[MAX_PATH];
578 static const char prefix[] = "pfx";
579 HANDLE hfile;
580 HANDLE hmapfile;
581 FILETIME ft1, ft2;
582 char buf[10];
583 DWORD ret;
584 BOOL retok;
585
586 ret = GetTempPathA(MAX_PATH, temp_path);
587 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
588 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
589
590 ret = GetTempFileNameA(temp_path, prefix, 0, source);
591 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
592
593 ret = MoveFileA(source, source);
594 todo_wine ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
595
596 /* copying a file to itself must fail */
597 retok = CopyFileA(source, source, FALSE);
598 ok( !retok && (GetLastError() == ERROR_SHARING_VIOLATION || broken(GetLastError() == ERROR_FILE_EXISTS) /* Win 9x */),
599 "copying a file to itself didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
600
601 /* make the source have not zero size */
602 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
603 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
604 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
605 ok( retok && ret == sizeof(prefix),
606 "WriteFile error %d\n", GetLastError());
607 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
608 /* get the file time and change it to prove the difference */
609 ret = GetFileTime(hfile, NULL, NULL, &ft1);
610 ok( ret, "GetFileTime error %d\n", GetLastError());
611 ft1.dwLowDateTime -= 600000000; /* 60 second */
612 ret = SetFileTime(hfile, NULL, NULL, &ft1);
613 ok( ret, "SetFileTime error %d\n", GetLastError());
614 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
615 CloseHandle(hfile);
616
617 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
618 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
619
620 SetLastError(0xdeadbeef);
621 ret = CopyFileA(source, dest, TRUE);
622 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
623 "CopyFileA: unexpected error %d\n", GetLastError());
624
625 ret = CopyFileA(source, dest, FALSE);
626 ok(ret, "CopyFileA: error %d\n", GetLastError());
627
628 /* copying from a read-locked source fails */
629 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
630 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
631 retok = CopyFileA(source, dest, FALSE);
632 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
633 "copying from a read-locked file succeeded when it shouldn't have\n");
634 /* in addition, the source is opened before the destination */
635 retok = CopyFileA("25f99d3b-4ba4-4f66-88f5-2906886993cc", dest, FALSE);
636 ok(!retok && GetLastError() == ERROR_FILE_NOT_FOUND,
637 "copying from a file that doesn't exist failed in an unexpected way (ret=%d, err=%d)\n", retok, GetLastError());
638 CloseHandle(hfile);
639
640 /* copying from a r+w opened, r shared source succeeds */
641 hfile = CreateFileA(source, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
642 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
643 retok = CopyFileA(source, dest, FALSE);
644 ok(retok,
645 "copying from an r+w opened and r shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
646 CloseHandle(hfile);
647
648 /* copying from a delete-locked source is unreliable */
649 hfile = CreateFileA(source, DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
650 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file, error %d\n", GetLastError());
651 retok = CopyFileA(source, dest, FALSE);
652 ok((!retok && GetLastError() == ERROR_SHARING_VIOLATION) || broken(retok) /* 98, Vista, 2k8, 7 */,
653 "copying from a delete-locked file failed (ret=%d, err=%d)\n", retok, GetLastError());
654 CloseHandle(hfile);
655
656 /* copying to a write-locked destination fails */
657 hfile = CreateFileA(dest, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
658 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
659 retok = CopyFileA(source, dest, FALSE);
660 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
661 "copying to a write-locked file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
662 CloseHandle(hfile);
663
664 /* copying to a r+w opened, w shared destination mostly succeeds */
665 hfile = CreateFileA(dest, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
666 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
667 retok = CopyFileA(source, dest, FALSE);
668 ok(retok || broken(!retok && GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
669 "copying to a r+w opened and w shared file failed (ret=%d, err=%d)\n", retok, GetLastError());
670 CloseHandle(hfile);
671
672 /* copying to a delete-locked destination fails, even when the destination is delete-shared */
673 hfile = CreateFileA(dest, DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, 0);
674 ok(hfile != INVALID_HANDLE_VALUE || broken(GetLastError() == ERROR_INVALID_PARAMETER) /* Win 9x */,
675 "failed to open destination file, error %d\n", GetLastError());
676 if (hfile != INVALID_HANDLE_VALUE)
677 {
678 retok = CopyFileA(source, dest, FALSE);
679 ok(!retok && GetLastError() == ERROR_SHARING_VIOLATION,
680 "copying to a delete-locked shared file didn't fail (ret=%d, err=%d)\n", retok, GetLastError());
681 CloseHandle(hfile);
682 }
683
684 /* copy to a file that's opened the way Wine opens the source */
685 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
686 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file, error %d\n", GetLastError());
687 retok = CopyFileA(source, dest, FALSE);
688 ok(retok || broken(GetLastError() == ERROR_SHARING_VIOLATION) /* Win 9x */,
689 "copying to a file opened the way Wine opens the source failed (ret=%d, err=%d)\n", retok, GetLastError());
690 CloseHandle(hfile);
691
692 /* make sure that destination has correct size */
693 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
694 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
695 ret = GetFileSize(hfile, NULL);
696 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
697
698 /* make sure that destination has the same filetime */
699 ret = GetFileTime(hfile, NULL, NULL, &ft2);
700 ok( ret, "GetFileTime error %d\n", GetLastError());
701 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
702
703 SetLastError(0xdeadbeef);
704 ret = CopyFileA(source, dest, FALSE);
705 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
706 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
707
708 /* make sure that destination still has correct size */
709 ret = GetFileSize(hfile, NULL);
710 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
711 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
712 ok( retok && ret == sizeof(prefix),
713 "ReadFile: error %d\n", GetLastError());
714 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
715
716 /* check error on copying over a mapped file that was opened with FILE_SHARE_READ */
717 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
718 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
719
720 ret = CopyFileA(source, dest, FALSE);
721 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
722 "CopyFileA with mapped dest file: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
723
724 CloseHandle(hmapfile);
725 CloseHandle(hfile);
726
727 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
728 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
729
730 /* check error on copying over a mapped file that was opened with FILE_SHARE_WRITE */
731 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
732 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
733
734 ret = CopyFileA(source, dest, FALSE);
735 ok(!ret, "CopyFileA: expected failure\n");
736 ok(GetLastError() == ERROR_USER_MAPPED_FILE ||
737 broken(GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x */
738 "CopyFileA with mapped dest file: expected ERROR_USER_MAPPED_FILE, got %d\n", GetLastError());
739
740 CloseHandle(hmapfile);
741 CloseHandle(hfile);
742
743 ret = DeleteFileA(source);
744 ok(ret, "DeleteFileA: error %d\n", GetLastError());
745 ret = DeleteFileA(dest);
746 ok(ret, "DeleteFileA: error %d\n", GetLastError());
747 }
748
749 static void test_CopyFileW(void)
750 {
751 WCHAR temp_path[MAX_PATH];
752 WCHAR source[MAX_PATH], dest[MAX_PATH];
753 static const WCHAR prefix[] = {'p','f','x',0};
754 DWORD ret;
755
756 ret = GetTempPathW(MAX_PATH, temp_path);
757 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
758 {
759 win_skip("GetTempPathW is not available\n");
760 return;
761 }
762 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
763 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
764
765 ret = GetTempFileNameW(temp_path, prefix, 0, source);
766 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
767
768 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
769 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
770
771 ret = CopyFileW(source, dest, TRUE);
772 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
773 "CopyFileW: unexpected error %d\n", GetLastError());
774
775 ret = CopyFileW(source, dest, FALSE);
776 ok(ret, "CopyFileW: error %d\n", GetLastError());
777
778 ret = DeleteFileW(source);
779 ok(ret, "DeleteFileW: error %d\n", GetLastError());
780 ret = DeleteFileW(dest);
781 ok(ret, "DeleteFileW: error %d\n", GetLastError());
782 }
783
784
785 /*
786 * Debugging routine to dump a buffer in a hexdump-like fashion.
787 */
788 static void dumpmem(unsigned char *mem, int len)
789 {
790 int x = 0;
791 char hex[49], *p;
792 char txt[17], *c;
793
794 while (x < len)
795 {
796 p = hex;
797 c = txt;
798 do {
799 p += sprintf(p, "%02hhx ", mem[x]);
800 *c++ = (mem[x] >= 32 && mem[x] <= 127) ? mem[x] : '.';
801 } while (++x % 16 && x < len);
802 *c = '\0';
803 trace("%04x: %-48s- %s\n", x, hex, txt);
804 }
805 }
806
807 static void test_CreateFileA(void)
808 {
809 HANDLE hFile;
810 char temp_path[MAX_PATH], dirname[MAX_PATH];
811 char filename[MAX_PATH];
812 static const char prefix[] = "pfx";
813 char windowsdir[MAX_PATH];
814 char Volume_1[MAX_PATH];
815 unsigned char buffer[512];
816 char directory[] = "removeme";
817 static const char nt_drive[] = "\\\\?\\A:";
818 DWORD i, ret, len;
819 struct test_list p[] = {
820 {"", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dir as file w \ */
821 {"", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* dir as dir w \ */
822 {"a", ERROR_FILE_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist file */
823 {"a\\", ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* non-exist dir */
824 {"removeme", ERROR_ACCESS_DENIED, -1, FILE_ATTRIBUTE_NORMAL, FALSE }, /* exist dir w/o \ */
825 {"removeme\\", ERROR_PATH_NOT_FOUND, -1, FILE_ATTRIBUTE_NORMAL, TRUE }, /* exst dir w \ */
826 {"c:", ERROR_ACCESS_DENIED, ERROR_PATH_NOT_FOUND, FILE_ATTRIBUTE_NORMAL, FALSE }, /* device in file namespace */
827 {"c:", ERROR_SUCCESS, ERROR_PATH_NOT_FOUND, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* device in file namespace as dir */
828 {"c:\\", ERROR_PATH_NOT_FOUND, ERROR_ACCESS_DENIED, FILE_ATTRIBUTE_NORMAL, TRUE }, /* root dir w \ */
829 {"c:\\", ERROR_SUCCESS, ERROR_ACCESS_DENIED, FILE_FLAG_BACKUP_SEMANTICS, FALSE }, /* root dir w \ as dir */
830 {"\\\\?\\c:", ERROR_SUCCESS, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL,FALSE }, /* dev namespace drive */
831 {"\\\\?\\c:\\", ERROR_PATH_NOT_FOUND, ERROR_BAD_NETPATH, FILE_ATTRIBUTE_NORMAL, TRUE }, /* dev namespace drive w \ */
832 {NULL, 0, -1, 0, FALSE}
833 };
834 BY_HANDLE_FILE_INFORMATION Finfo;
835
836 ret = GetTempPathA(MAX_PATH, temp_path);
837 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
838 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
839
840 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
841 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
842
843 SetLastError(0xdeadbeef);
844 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
845 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
846 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
847 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
848
849 SetLastError(0xdeadbeef);
850 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
851 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
852 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
853 "hFile %p, last error %u\n", hFile, GetLastError());
854
855 CloseHandle(hFile);
856
857 SetLastError(0xdeadbeef);
858 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
859 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
860 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
861 "hFile %p, last error %u\n", hFile, GetLastError());
862
863 CloseHandle(hFile);
864
865 ret = DeleteFileA(filename);
866 ok(ret, "DeleteFileA: error %d\n", GetLastError());
867
868 SetLastError(0xdeadbeef);
869 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
870 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
871 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
872 "hFile %p, last error %u\n", hFile, GetLastError());
873
874 CloseHandle(hFile);
875
876 ret = DeleteFileA(filename);
877 ok(ret, "DeleteFileA: error %d\n", GetLastError());
878
879 SetLastError(0xdeadbeef);
880 hFile = CreateFileA("c:\\*.*", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
881 ok(hFile == INVALID_HANDLE_VALUE, "hFile should have been INVALID_HANDLE_VALUE\n");
882 ok(GetLastError() == ERROR_INVALID_NAME ||
883 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* Win98 */
884 "LastError should have been ERROR_INVALID_NAME or ERROR_FILE_NOT_FOUND but got %u\n", GetLastError());
885
886 /* get windows drive letter */
887 ret = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
888 ok(ret < sizeof(windowsdir), "windowsdir is abnormally long!\n");
889 ok(ret != 0, "GetWindowsDirectory: error %d\n", GetLastError());
890
891 /* test error return codes from CreateFile for some cases */
892 ret = GetTempPathA(MAX_PATH, temp_path);
893 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
894 strcpy(dirname, temp_path);
895 strcat(dirname, directory);
896 ret = CreateDirectory(dirname, NULL);
897 ok( ret, "Createdirectory failed, gle=%d\n", GetLastError() );
898 /* set current drive & directory to known location */
899 SetCurrentDirectoryA( temp_path );
900 i = 0;
901 while (p[i].file)
902 {
903 filename[0] = 0;
904 /* update the drive id in the table entry with the current one */
905 if (p[i].file[1] == ':')
906 {
907 strcpy(filename, p[i].file);
908 filename[0] = windowsdir[0];
909 }
910 else if (p[i].file[0] == '\\' && p[i].file[5] == ':')
911 {
912 strcpy(filename, p[i].file);
913 filename[4] = windowsdir[0];
914 }
915 else
916 {
917 /* prefix the table entry with the current temp directory */
918 strcpy(filename, temp_path);
919 strcat(filename, p[i].file);
920 }
921 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
922 FILE_SHARE_READ | FILE_SHARE_WRITE,
923 NULL, OPEN_EXISTING,
924 p[i].options, NULL );
925 /* if we get ACCESS_DENIED when we do not expect it, assume
926 * no access to the volume
927 */
928 if (hFile == INVALID_HANDLE_VALUE &&
929 GetLastError() == ERROR_ACCESS_DENIED &&
930 p[i].err != ERROR_ACCESS_DENIED)
931 {
932 if (p[i].todo_flag)
933 skip("Either no authority to volume, or is todo_wine for %s err=%d should be %d\n", filename, GetLastError(), p[i].err);
934 else
935 skip("Do not have authority to access volumes. Test for %s skipped\n", filename);
936 }
937 /* otherwise validate results with expectations */
938 else if (p[i].todo_flag)
939 todo_wine ok(
940 (hFile == INVALID_HANDLE_VALUE &&
941 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
942 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
943 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
944 filename, hFile, GetLastError(), p[i].err);
945 else
946 ok(
947 (hFile == INVALID_HANDLE_VALUE &&
948 (p[i].err == GetLastError() || p[i].err2 == GetLastError())) ||
949 (hFile != INVALID_HANDLE_VALUE && p[i].err == ERROR_SUCCESS),
950 "CreateFileA failed on %s, hFile %p, err=%u, should be %u\n",
951 filename, hFile, GetLastError(), p[i].err);
952 if (hFile != INVALID_HANDLE_VALUE)
953 CloseHandle( hFile );
954 i++;
955 }
956 ret = RemoveDirectoryA(dirname);
957 ok(ret, "RemoveDirectoryA: error %d\n", GetLastError());
958
959
960 /* test opening directory as a directory */
961 hFile = CreateFileA( temp_path, GENERIC_READ,
962 FILE_SHARE_READ,
963 NULL,
964 OPEN_EXISTING,
965 FILE_FLAG_BACKUP_SEMANTICS, NULL );
966 if (hFile != INVALID_HANDLE_VALUE && GetLastError() != ERROR_PATH_NOT_FOUND)
967 {
968 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_SUCCESS,
969 "CreateFileA did not work, last error %u on volume <%s>\n",
970 GetLastError(), temp_path );
971
972 if (hFile != INVALID_HANDLE_VALUE)
973 {
974 ret = GetFileInformationByHandle( hFile, &Finfo );
975 if (ret)
976 {
977 ok(Finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY,
978 "CreateFileA probably did not open temp directory %s correctly\n file information does not include FILE_ATTRIBUTE_DIRECTORY, actual=0x%08x\n",
979 temp_path, Finfo.dwFileAttributes);
980 }
981 CloseHandle( hFile );
982 }
983 }
984 else
985 skip("Probable Win9x, got ERROR_PATH_NOT_FOUND w/ FILE_FLAG_BACKUP_SEMANTICS or %s\n", temp_path);
986
987
988 /* *** Test opening volumes/devices using drive letter *** */
989
990 /* test using drive letter in non-rewrite format without trailing \ */
991 /* this should work */
992 strcpy(filename, nt_drive);
993 filename[4] = windowsdir[0];
994 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
995 FILE_SHARE_READ | FILE_SHARE_WRITE,
996 NULL, OPEN_EXISTING,
997 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
998 if (hFile != INVALID_HANDLE_VALUE ||
999 (GetLastError() != ERROR_ACCESS_DENIED && GetLastError() != ERROR_BAD_NETPATH))
1000 {
1001 /* if we have adm rights to volume, then try rest of tests */
1002 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1003 filename, GetLastError());
1004 if (hFile != INVALID_HANDLE_VALUE)
1005 {
1006 /* if we opened the volume/device, try to read it. Since it */
1007 /* opened, we should be able to read it. We don't care about*/
1008 /* what the data is at this time. */
1009 len = 512;
1010 ret = ReadFile( hFile, buffer, len, &len, NULL );
1011 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1012 GetLastError(), ret, filename);
1013 if (ret)
1014 {
1015 trace("buffer is\n");
1016 dumpmem(buffer, 64);
1017 }
1018 CloseHandle( hFile );
1019 }
1020
1021 /* test using drive letter with trailing \ and in non-rewrite */
1022 /* this should not work */
1023 strcpy(filename, nt_drive);
1024 filename[4] = windowsdir[0];
1025 strcat( filename, "\\" );
1026 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1027 FILE_SHARE_READ | FILE_SHARE_WRITE,
1028 NULL, OPEN_EXISTING,
1029 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1030 todo_wine
1031 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1032 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1033 filename, GetLastError());
1034 if (hFile != INVALID_HANDLE_VALUE)
1035 CloseHandle( hFile );
1036
1037 /* test using temp path with trailing \ and in non-rewrite as dir */
1038 /* this should work */
1039 strcpy(filename, nt_drive);
1040 filename[4] = 0;
1041 strcat( filename, temp_path );
1042 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1043 FILE_SHARE_READ | FILE_SHARE_WRITE,
1044 NULL, OPEN_EXISTING,
1045 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1046 ok(hFile != INVALID_HANDLE_VALUE,
1047 "CreateFileA should have worked on %s, but got %u\n",
1048 filename, GetLastError());
1049 if (hFile != INVALID_HANDLE_VALUE)
1050 CloseHandle( hFile );
1051
1052 /* test using drive letter without trailing \ and in device ns */
1053 /* this should work */
1054 strcpy(filename, nt_drive);
1055 filename[4] = windowsdir[0];
1056 filename[2] = '.';
1057 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1058 FILE_SHARE_READ | FILE_SHARE_WRITE,
1059 NULL, OPEN_EXISTING,
1060 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1061 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1062 filename, GetLastError());
1063 if (hFile != INVALID_HANDLE_VALUE)
1064 CloseHandle( hFile );
1065 }
1066 /* If we see ERROR_BAD_NETPATH then on Win9x or WinME, so skip */
1067 else if (GetLastError() == ERROR_BAD_NETPATH)
1068 skip("Probable Win9x, got ERROR_BAD_NETPATH (53)\n");
1069 else
1070 skip("Do not have authority to access volumes. Tests skipped\n");
1071
1072
1073 /* *** Test opening volumes/devices using GUID *** */
1074
1075 if (pGetVolumeNameForVolumeMountPointA)
1076 {
1077 strcpy(filename, "c:\\");
1078 filename[0] = windowsdir[0];
1079 ret = pGetVolumeNameForVolumeMountPointA( filename, Volume_1, MAX_PATH );
1080 ok(ret, "GetVolumeNameForVolumeMountPointA failed, for %s, last error=%d\n", filename, GetLastError());
1081 if (ret)
1082 {
1083 ok(strlen(Volume_1) == 49, "GetVolumeNameForVolumeMountPointA returned wrong length name <%s>\n", Volume_1);
1084
1085 /* test the result of opening a unique volume name (GUID)
1086 * with the trailing \
1087 * this should error out
1088 */
1089 strcpy(filename, Volume_1);
1090 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1091 FILE_SHARE_READ | FILE_SHARE_WRITE,
1092 NULL, OPEN_EXISTING,
1093 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1094 todo_wine
1095 ok(hFile == INVALID_HANDLE_VALUE,
1096 "CreateFileA should not have opened %s, hFile %p\n",
1097 filename, hFile);
1098 todo_wine
1099 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1100 "CreateFileA should have returned ERROR_PATH_NOT_FOUND on %s, but got %u\n",
1101 filename, GetLastError());
1102 if (hFile != INVALID_HANDLE_VALUE)
1103 CloseHandle( hFile );
1104
1105 /* test the result of opening a unique volume name (GUID)
1106 * with the temp path string as dir
1107 * this should work
1108 */
1109 strcpy(filename, Volume_1);
1110 strcat(filename, temp_path+3);
1111 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1112 FILE_SHARE_READ | FILE_SHARE_WRITE,
1113 NULL, OPEN_EXISTING,
1114 FILE_FLAG_BACKUP_SEMANTICS, NULL );
1115 todo_wine
1116 ok(hFile != INVALID_HANDLE_VALUE,
1117 "CreateFileA should have opened %s, but got %u\n",
1118 filename, GetLastError());
1119 if (hFile != INVALID_HANDLE_VALUE)
1120 CloseHandle( hFile );
1121
1122 /* test the result of opening a unique volume name (GUID)
1123 * without the trailing \ and in device namespace
1124 * this should work
1125 */
1126 strcpy(filename, Volume_1);
1127 filename[2] = '.';
1128 filename[48] = 0;
1129 hFile = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1130 FILE_SHARE_READ | FILE_SHARE_WRITE,
1131 NULL, OPEN_EXISTING,
1132 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL );
1133 if (hFile != INVALID_HANDLE_VALUE || GetLastError() != ERROR_ACCESS_DENIED)
1134 {
1135 /* if we have adm rights to volume, then try rest of tests */
1136 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA did not open %s, last error=%u\n",
1137 filename, GetLastError());
1138 if (hFile != INVALID_HANDLE_VALUE)
1139 {
1140 /* if we opened the volume/device, try to read it. Since it */
1141 /* opened, we should be able to read it. We don't care about*/
1142 /* what the data is at this time. */
1143 len = 512;
1144 ret = ReadFile( hFile, buffer, len, &len, NULL );
1145 todo_wine ok(ret, "Failed to read volume, last error %u, %u, for %s\n",
1146 GetLastError(), ret, filename);
1147 if (ret)
1148 {
1149 trace("buffer is\n");
1150 dumpmem(buffer, 64);
1151 }
1152 CloseHandle( hFile );
1153 }
1154 }
1155 else
1156 skip("Do not have authority to access volumes. Tests skipped\n");
1157 }
1158 else
1159 win_skip("GetVolumeNameForVolumeMountPointA not functioning\n");
1160 }
1161 else
1162 win_skip("GetVolumeNameForVolumeMountPointA not found\n");
1163 }
1164
1165 static void test_CreateFileW(void)
1166 {
1167 HANDLE hFile;
1168 WCHAR temp_path[MAX_PATH];
1169 WCHAR filename[MAX_PATH];
1170 static const WCHAR emptyW[]={'\0'};
1171 static const WCHAR prefix[] = {'p','f','x',0};
1172 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
1173 DWORD ret;
1174
1175 ret = GetTempPathW(MAX_PATH, temp_path);
1176 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1177 {
1178 win_skip("GetTempPathW is not available\n");
1179 return;
1180 }
1181 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1182 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1183
1184 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
1185 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1186
1187 SetLastError(0xdeadbeef);
1188 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
1189 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1190 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
1191 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
1192
1193 SetLastError(0xdeadbeef);
1194 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1195 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1196 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1197 "hFile %p, last error %u\n", hFile, GetLastError());
1198
1199 CloseHandle(hFile);
1200
1201 SetLastError(0xdeadbeef);
1202 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1203 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1204 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
1205 "hFile %p, last error %u\n", hFile, GetLastError());
1206
1207 CloseHandle(hFile);
1208
1209 ret = DeleteFileW(filename);
1210 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1211
1212 SetLastError(0xdeadbeef);
1213 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1214 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
1215 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
1216 "hFile %p, last error %u\n", hFile, GetLastError());
1217
1218 CloseHandle(hFile);
1219
1220 ret = DeleteFileW(filename);
1221 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1222
1223 if (0)
1224 {
1225 /* this crashes on NT4.0 */
1226 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
1227 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1228 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1229 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
1230 }
1231
1232 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
1233 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
1234 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
1235 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
1236
1237 /* test the result of opening a nonexistent driver name */
1238 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1239 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1240 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
1241 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFile,GetLastError());
1242
1243 ret = CreateDirectoryW(filename, NULL);
1244 ok(ret == TRUE, "couldn't create temporary directory\n");
1245 hFile = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1246 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
1247 ok(hFile != INVALID_HANDLE_VALUE,
1248 "expected CreateFile to succeed on existing directory, error: %d\n", GetLastError());
1249 CloseHandle(hFile);
1250 ret = RemoveDirectoryW(filename);
1251 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1252 }
1253
1254 static void test_GetTempFileNameA(void)
1255 {
1256 UINT result;
1257 char out[MAX_PATH];
1258 char expected[MAX_PATH + 10];
1259 char windowsdir[MAX_PATH + 10];
1260 char windowsdrive[3];
1261
1262 result = GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1263 ok(result < sizeof(windowsdir), "windowsdir is abnormally long!\n");
1264 ok(result != 0, "GetWindowsDirectory: error %d\n", GetLastError());
1265
1266 /* If the Windows directory is the root directory, it ends in backslash, not else. */
1267 if (strlen(windowsdir) != 3) /* As in "C:\" or "F:\" */
1268 {
1269 strcat(windowsdir, "\\");
1270 }
1271
1272 windowsdrive[0] = windowsdir[0];
1273 windowsdrive[1] = windowsdir[1];
1274 windowsdrive[2] = '\0';
1275
1276 result = GetTempFileNameA(windowsdrive, "abc", 1, out);
1277 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1278 ok(((out[0] == windowsdrive[0]) && (out[1] == ':')) && (out[2] == '\\'),
1279 "GetTempFileNameA: first three characters should be %c:\\, string was actually %s\n",
1280 windowsdrive[0], out);
1281
1282 result = GetTempFileNameA(windowsdir, "abc", 2, out);
1283 ok(result != 0, "GetTempFileNameA: error %d\n", GetLastError());
1284 expected[0] = '\0';
1285 strcat(expected, windowsdir);
1286 strcat(expected, "abc2.tmp");
1287 ok(lstrcmpiA(out, expected) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n",
1288 out, expected);
1289 }
1290
1291 static void test_DeleteFileA( void )
1292 {
1293 BOOL ret;
1294
1295 ret = DeleteFileA(NULL);
1296 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
1297 GetLastError() == ERROR_PATH_NOT_FOUND),
1298 "DeleteFileA(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1299
1300 ret = DeleteFileA("");
1301 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
1302 GetLastError() == ERROR_BAD_PATHNAME),
1303 "DeleteFileA(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1304
1305 ret = DeleteFileA("nul");
1306 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
1307 GetLastError() == ERROR_INVALID_PARAMETER ||
1308 GetLastError() == ERROR_ACCESS_DENIED ||
1309 GetLastError() == ERROR_INVALID_FUNCTION),
1310 "DeleteFileA(\"nul\") returned ret=%d error=%d\n",ret,GetLastError());
1311 }
1312
1313 static void test_DeleteFileW( void )
1314 {
1315 BOOL ret;
1316 WCHAR pathW[MAX_PATH];
1317 WCHAR pathsubW[MAX_PATH];
1318 static const WCHAR dirW[] = {'d','e','l','e','t','e','f','i','l','e',0};
1319 static const WCHAR subdirW[] = {'\\','s','u','b',0};
1320 static const WCHAR emptyW[]={'\0'};
1321
1322 ret = DeleteFileW(NULL);
1323 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1324 {
1325 win_skip("DeleteFileW is not available\n");
1326 return;
1327 }
1328 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1329 "DeleteFileW(NULL) returned ret=%d error=%d\n",ret,GetLastError());
1330
1331 ret = DeleteFileW(emptyW);
1332 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
1333 "DeleteFileW(\"\") returned ret=%d error=%d\n",ret,GetLastError());
1334
1335 /* test DeleteFile on empty directory */
1336 ret = GetTempPathW(MAX_PATH, pathW);
1337 if (ret + sizeof(dirW)/sizeof(WCHAR)-1 + sizeof(subdirW)/sizeof(WCHAR)-1 >= MAX_PATH)
1338 {
1339 ok(0, "MAX_PATH exceeded in constructing paths\n");
1340 return;
1341 }
1342 lstrcatW(pathW, dirW);
1343 lstrcpyW(pathsubW, pathW);
1344 lstrcatW(pathsubW, subdirW);
1345 ret = CreateDirectoryW(pathW, NULL);
1346 ok(ret == TRUE, "couldn't create directory deletefile\n");
1347 ret = DeleteFileW(pathW);
1348 ok(ret == FALSE, "DeleteFile should fail for empty directories\n");
1349 ret = RemoveDirectoryW(pathW);
1350 ok(ret == TRUE, "expected to remove directory deletefile\n");
1351
1352 /* test DeleteFile on non-empty directory */
1353 ret = CreateDirectoryW(pathW, NULL);
1354 ok(ret == TRUE, "couldn't create directory deletefile\n");
1355 ret = CreateDirectoryW(pathsubW, NULL);
1356 ok(ret == TRUE, "couldn't create directory deletefile\\sub\n");
1357 ret = DeleteFileW(pathW);
1358 ok(ret == FALSE, "DeleteFile should fail for non-empty directories\n");
1359 ret = RemoveDirectoryW(pathsubW);
1360 ok(ret == TRUE, "expected to remove directory deletefile\\sub\n");
1361 ret = RemoveDirectoryW(pathW);
1362 ok(ret == TRUE, "expected to remove directory deletefile\n");
1363 }
1364
1365 #define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
1366
1367 static void test_MoveFileA(void)
1368 {
1369 char tempdir[MAX_PATH];
1370 char source[MAX_PATH], dest[MAX_PATH];
1371 static const char prefix[] = "pfx";
1372 HANDLE hfile;
1373 HANDLE hmapfile;
1374 DWORD ret;
1375 BOOL retok;
1376
1377 ret = GetTempPathA(MAX_PATH, tempdir);
1378 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
1379 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1380
1381 ret = GetTempFileNameA(tempdir, prefix, 0, source);
1382 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1383
1384 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
1385 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
1386
1387 ret = MoveFileA(source, dest);
1388 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1389 "MoveFileA: unexpected error %d\n", GetLastError());
1390
1391 ret = DeleteFileA(dest);
1392 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1393
1394 hfile = CreateFileA(source, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
1395 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1396
1397 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
1398 ok( retok && ret == sizeof(prefix),
1399 "WriteFile error %d\n", GetLastError());
1400
1401 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1402 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1403
1404 ret = MoveFileA(source, dest);
1405 todo_wine {
1406 ok(!ret, "MoveFileA: expected failure\n");
1407 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1408 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1409 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1410 }
1411
1412 CloseHandle(hmapfile);
1413 CloseHandle(hfile);
1414
1415 /* if MoveFile succeeded, move back to dest */
1416 if (ret) MoveFile(dest, source);
1417
1418 hfile = CreateFileA(source, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1419 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
1420
1421 hmapfile = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
1422 ok(hmapfile != NULL, "CreateFileMapping: error %d\n", GetLastError());
1423
1424 ret = MoveFileA(source, dest);
1425 todo_wine {
1426 ok(!ret, "MoveFileA: expected failure\n");
1427 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
1428 broken(GetLastError() == ERROR_ACCESS_DENIED), /* Win9x and WinMe */
1429 "MoveFileA: expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
1430 }
1431
1432 CloseHandle(hmapfile);
1433 CloseHandle(hfile);
1434
1435 /* if MoveFile succeeded, move back to dest */
1436 if (ret) MoveFile(dest, source);
1437
1438 ret = MoveFileA(source, dest);
1439 ok(ret, "MoveFileA: failed, error %d\n", GetLastError());
1440
1441 lstrcatA(tempdir, "Remove Me");
1442 ret = CreateDirectoryA(tempdir, NULL);
1443 ok(ret == TRUE, "CreateDirectoryA failed\n");
1444
1445 lstrcpyA(source, dest);
1446 lstrcpyA(dest, tempdir);
1447 lstrcatA(dest, "\\wild?.*");
1448 /* FIXME: if we create a file with wildcards we can't delete it now that DeleteFile works correctly */
1449 ret = MoveFileA(source, dest);
1450 ok(!ret, "MoveFileA: shouldn't move to wildcard file\n");
1451 ok(GetLastError() == ERROR_INVALID_NAME || /* NT */
1452 GetLastError() == ERROR_FILE_NOT_FOUND, /* Win9x */
1453 "MoveFileA: with wildcards, unexpected error %d\n", GetLastError());
1454 if (ret || (GetLastError() != ERROR_INVALID_NAME))
1455 {
1456 WIN32_FIND_DATAA fd;
1457 char temppath[MAX_PATH];
1458 HANDLE hFind;
1459
1460 lstrcpyA(temppath, tempdir);
1461 lstrcatA(temppath, "\\*.*");
1462 hFind = FindFirstFileA(temppath, &fd);
1463 if (INVALID_HANDLE_VALUE != hFind)
1464 {
1465 LPSTR lpName;
1466 do
1467 {
1468 lpName = fd.cAlternateFileName;
1469 if (!lpName[0])
1470 lpName = fd.cFileName;
1471 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!\n");
1472 }
1473 while (FindNextFileA(hFind, &fd));
1474 FindClose(hFind);
1475 }
1476 }
1477 ret = DeleteFileA(source);
1478 ok(ret, "DeleteFileA: error %d\n", GetLastError());
1479 ret = DeleteFileA(dest);
1480 ok(!ret, "DeleteFileA: error %d\n", GetLastError());
1481 ret = RemoveDirectoryA(tempdir);
1482 ok(ret, "DeleteDirectoryA: error %d\n", GetLastError());
1483 }
1484
1485 static void test_MoveFileW(void)
1486 {
1487 WCHAR temp_path[MAX_PATH];
1488 WCHAR source[MAX_PATH], dest[MAX_PATH];
1489 static const WCHAR prefix[] = {'p','f','x',0};
1490 DWORD ret;
1491
1492 ret = GetTempPathW(MAX_PATH, temp_path);
1493 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1494 {
1495 win_skip("GetTempPathW is not available\n");
1496 return;
1497 }
1498 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
1499 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
1500
1501 ret = GetTempFileNameW(temp_path, prefix, 0, source);
1502 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1503
1504 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
1505 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
1506
1507 ret = MoveFileW(source, dest);
1508 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
1509 "CopyFileW: unexpected error %d\n", GetLastError());
1510
1511 ret = DeleteFileW(source);
1512 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1513 ret = DeleteFileW(dest);
1514 ok(ret, "DeleteFileW: error %d\n", GetLastError());
1515 }
1516
1517 #define PATTERN_OFFSET 0x10
1518
1519 static void test_offset_in_overlapped_structure(void)
1520 {
1521 HANDLE hFile;
1522 OVERLAPPED ov;
1523 DWORD done, offset;
1524 BOOL rc;
1525 BYTE buf[256], pattern[] = "TeSt";
1526 UINT i;
1527 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
1528 BOOL ret;
1529
1530 ret =GetTempPathA(MAX_PATH, temp_path);
1531 ok( ret, "GetTempPathA error %d\n", GetLastError());
1532 ret =GetTempFileNameA(temp_path, "pfx", 0, temp_fname);
1533 ok( ret, "GetTempFileNameA error %d\n", GetLastError());
1534
1535 /*** Write File *****************************************************/
1536
1537 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1538 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1539
1540 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
1541 ret = WriteFile(hFile, buf, sizeof(buf), &done, NULL);
1542 ok( ret, "WriteFile error %d\n", GetLastError());
1543 ok(done == sizeof(buf), "expected number of bytes written %u\n", done);
1544
1545 memset(&ov, 0, sizeof(ov));
1546 S(U(ov)).Offset = PATTERN_OFFSET;
1547 S(U(ov)).OffsetHigh = 0;
1548 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1549 /* Win 9x does not support the overlapped I/O on files */
1550 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1551 ok(rc, "WriteFile error %d\n", GetLastError());
1552 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1553 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1554 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1555
1556 S(U(ov)).Offset = sizeof(buf) * 2;
1557 S(U(ov)).OffsetHigh = 0;
1558 ret = WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
1559 ok( ret, "WriteFile error %d\n", GetLastError());
1560 ok(done == sizeof(pattern), "expected number of bytes written %u\n", done);
1561 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1562 ok(offset == sizeof(buf) * 2 + sizeof(pattern), "wrong file offset %d\n", offset);
1563 }
1564
1565 CloseHandle(hFile);
1566
1567 /*** Read File *****************************************************/
1568
1569 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
1570 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError());
1571
1572 memset(buf, 0, sizeof(buf));
1573 memset(&ov, 0, sizeof(ov));
1574 S(U(ov)).Offset = PATTERN_OFFSET;
1575 S(U(ov)).OffsetHigh = 0;
1576 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
1577 /* Win 9x does not support the overlapped I/O on files */
1578 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
1579 ok(rc, "ReadFile error %d\n", GetLastError());
1580 ok(done == sizeof(pattern), "expected number of bytes read %u\n", done);
1581 offset = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
1582 ok(offset == PATTERN_OFFSET + sizeof(pattern), "wrong file offset %d\n", offset);
1583 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed\n");
1584 }
1585
1586 CloseHandle(hFile);
1587
1588 ret = DeleteFileA(temp_fname);
1589 ok( ret, "DeleteFileA error %d\n", GetLastError());
1590 }
1591
1592 static void test_LockFile(void)
1593 {
1594 HANDLE handle;
1595 DWORD written;
1596 OVERLAPPED overlapped;
1597 int limited_LockFile;
1598 int limited_UnLockFile;
1599 BOOL ret;
1600
1601 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
1602 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1603 CREATE_ALWAYS, 0, 0 );
1604 if (handle == INVALID_HANDLE_VALUE)
1605 {
1606 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1607 return;
1608 }
1609 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed\n" );
1610
1611 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed\n" );
1612 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed\n" );
1613
1614 limited_UnLockFile = 0;
1615 if (UnlockFile( handle, 0, 0, 0, 0 ))
1616 {
1617 limited_UnLockFile = 1;
1618 }
1619
1620 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed\n" );
1621 /* overlapping locks must fail */
1622 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded\n" );
1623 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded\n" );
1624 /* non-overlapping locks must succeed */
1625 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed\n" );
1626
1627 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded\n" );
1628 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed\n" );
1629 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded\n" );
1630 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed\n" );
1631
1632 S(U(overlapped)).Offset = 100;
1633 S(U(overlapped)).OffsetHigh = 0;
1634 overlapped.hEvent = 0;
1635
1636 /* Test for broken LockFileEx a la Windows 95 OSR2. */
1637 if (LockFileEx( handle, 0, 0, 100, 0, &overlapped ))
1638 {
1639 /* LockFileEx is probably OK, test it more. */
1640 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ),
1641 "LockFileEx 100,100 failed\n" );
1642 }
1643
1644 /* overlapping shared locks are OK */
1645 S(U(overlapped)).Offset = 150;
1646 limited_UnLockFile || ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed\n" );
1647
1648 /* but exclusive is not */
1649 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
1650 0, 50, 0, &overlapped ),
1651 "LockFileEx exclusive 150,50 succeeded\n" );
1652 if (!UnlockFileEx( handle, 0, 100, 0, &overlapped ))
1653 { /* UnLockFile is capable. */
1654 S(U(overlapped)).Offset = 100;
1655 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ),
1656 "UnlockFileEx 150,100 again succeeded\n" );
1657 }
1658
1659 ret = LockFile( handle, 0, 0x10000000, 0, 0xf0000000 );
1660 if (ret)
1661 {
1662 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded\n" );
1663 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded\n" );
1664 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed\n" );
1665 }
1666 else /* win9x */
1667 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong LockFile error %u\n", GetLastError() );
1668
1669 /* wrap-around lock should not do anything */
1670 /* (but still succeeds on NT4 so we don't check result) */
1671 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
1672
1673 limited_LockFile = 0;
1674 if (!LockFile( handle, ~0, ~0, 1, 0 ))
1675 {
1676 limited_LockFile = 1;
1677 }
1678
1679 limited_UnLockFile || ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed\n" );
1680
1681 /* zero-byte lock */
1682 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed\n" );
1683 if (!limited_LockFile) ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded\n" );
1684 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed\n" );
1685 if (!limited_LockFile) ok( !LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed\n" );
1686
1687 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed\n" );
1688 ok( !UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 succeeded\n" );
1689
1690 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed\n" );
1691
1692 CloseHandle( handle );
1693 DeleteFileA( filename );
1694 }
1695
1696 static BOOL create_fake_dll( LPCSTR filename )
1697 {
1698 IMAGE_DOS_HEADER *dos;
1699 IMAGE_NT_HEADERS *nt;
1700 IMAGE_SECTION_HEADER *sec;
1701 BYTE *buffer;
1702 DWORD lfanew = sizeof(*dos);
1703 DWORD size = lfanew + sizeof(*nt) + sizeof(*sec);
1704 DWORD written;
1705 BOOL ret;
1706
1707 HANDLE file = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
1708 if (file == INVALID_HANDLE_VALUE) return FALSE;
1709
1710 buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
1711
1712 dos = (IMAGE_DOS_HEADER *)buffer;
1713 dos->e_magic = IMAGE_DOS_SIGNATURE;
1714 dos->e_cblp = sizeof(*dos);
1715 dos->e_cp = 1;
1716 dos->e_cparhdr = lfanew / 16;
1717 dos->e_minalloc = 0;
1718 dos->e_maxalloc = 0xffff;
1719 dos->e_ss = 0x0000;
1720 dos->e_sp = 0x00b8;
1721 dos->e_lfarlc = lfanew;
1722 dos->e_lfanew = lfanew;
1723
1724 nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
1725 nt->Signature = IMAGE_NT_SIGNATURE;
1726 #if defined __i386__
1727 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
1728 #elif defined __x86_64__
1729 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
1730 #elif defined __powerpc__
1731 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
1732 #elif defined __sparc__
1733 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_SPARC;
1734 #elif defined __arm__
1735 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM;
1736 #else
1737 # error You must specify the machine type
1738 #endif
1739 nt->FileHeader.NumberOfSections = 1;
1740 nt->FileHeader.SizeOfOptionalHeader = IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
1741 nt->FileHeader.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_EXECUTABLE_IMAGE;
1742 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
1743 nt->OptionalHeader.MajorLinkerVersion = 1;
1744 nt->OptionalHeader.MinorLinkerVersion = 0;
1745 nt->OptionalHeader.ImageBase = 0x10000000;
1746 nt->OptionalHeader.SectionAlignment = 0x1000;
1747 nt->OptionalHeader.FileAlignment = 0x1000;
1748 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
1749 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
1750 nt->OptionalHeader.MajorImageVersion = 1;
1751 nt->OptionalHeader.MinorImageVersion = 0;
1752 nt->OptionalHeader.MajorSubsystemVersion = 4;
1753 nt->OptionalHeader.MinorSubsystemVersion = 0;
1754 nt->OptionalHeader.SizeOfImage = 0x2000;
1755 nt->OptionalHeader.SizeOfHeaders = size;
1756 nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
1757 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1758
1759 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
1760 memcpy( sec->Name, ".rodata", sizeof(".rodata") );
1761 sec->Misc.VirtualSize = 0x1000;
1762 sec->VirtualAddress = 0x1000;
1763 sec->SizeOfRawData = 0;
1764 sec->PointerToRawData = 0;
1765 sec->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
1766
1767 ret = WriteFile( file, buffer, size, &written, NULL ) && written == size;
1768 HeapFree( GetProcessHeap(), 0, buffer );
1769 CloseHandle( file );
1770 return ret;
1771 }
1772
1773 static unsigned int map_file_access( unsigned int access )
1774 {
1775 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
1776 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
1777 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
1778 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
1779 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
1780 }
1781
1782 static int is_sharing_compatible( DWORD access1, DWORD sharing1, DWORD access2, DWORD sharing2 )
1783 {
1784 access1 = map_file_access( access1 );
1785 access2 = map_file_access( access2 );
1786 access1 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
1787 access2 &= FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_EXECUTE | DELETE;
1788
1789 if (!access1) sharing1 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1790 if (!access2) sharing2 = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
1791
1792 if ((access1 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing2 & FILE_SHARE_READ)) return 0;
1793 if ((access1 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing2 & FILE_SHARE_WRITE)) return 0;
1794 if ((access1 & DELETE) && !(sharing2 & FILE_SHARE_DELETE)) return 0;
1795 if ((access2 & (FILE_READ_DATA|FILE_EXECUTE)) && !(sharing1 & FILE_SHARE_READ)) return 0;
1796 if ((access2 & (FILE_WRITE_DATA|FILE_APPEND_DATA)) && !(sharing1 & FILE_SHARE_WRITE)) return 0;
1797 if ((access2 & DELETE) && !(sharing1 & FILE_SHARE_DELETE)) return 0;
1798 return 1;
1799 }
1800
1801 static int is_sharing_map_compatible( DWORD map_access, DWORD access2, DWORD sharing2 )
1802 {
1803 if ((map_access == PAGE_READWRITE || map_access == PAGE_EXECUTE_READWRITE) &&
1804 !(sharing2 & FILE_SHARE_WRITE)) return 0;
1805 access2 = map_file_access( access2 );
1806 if ((map_access & SEC_IMAGE) && (access2 & FILE_WRITE_DATA)) return 0;
1807 return 1;
1808 }
1809
1810 static void test_file_sharing(void)
1811 {
1812 static const DWORD access_modes[] =
1813 { 0, GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
1814 DELETE, GENERIC_READ|DELETE, GENERIC_WRITE|DELETE, GENERIC_READ|GENERIC_WRITE|DELETE,
1815 GENERIC_EXECUTE, GENERIC_EXECUTE | DELETE,
1816 FILE_READ_DATA, FILE_WRITE_DATA, FILE_APPEND_DATA, FILE_READ_EA, FILE_WRITE_EA,
1817 FILE_READ_DATA | FILE_EXECUTE, FILE_WRITE_DATA | FILE_EXECUTE, FILE_APPEND_DATA | FILE_EXECUTE,
1818 FILE_READ_EA | FILE_EXECUTE, FILE_WRITE_EA | FILE_EXECUTE, FILE_EXECUTE,
1819 FILE_DELETE_CHILD, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES };
1820 static const DWORD sharing_modes[] =
1821 { 0, FILE_SHARE_READ,
1822 FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
1823 FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_DELETE,
1824 FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE };
1825 static const DWORD mapping_modes[] =
1826 { PAGE_READONLY, PAGE_WRITECOPY, PAGE_READWRITE, SEC_IMAGE | PAGE_WRITECOPY };
1827 int a1, s1, a2, s2;
1828 int ret;
1829 HANDLE h, h2;
1830
1831 /* make sure the file exists */
1832 if (!create_fake_dll( filename ))
1833 {
1834 ok(0, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError());
1835 return;
1836 }
1837
1838 for (a1 = 0; a1 < sizeof(access_modes)/sizeof(access_modes[0]); a1++)
1839 {
1840 for (s1 = 0; s1 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s1++)
1841 {
1842 SetLastError(0xdeadbeef);
1843 h = CreateFileA( filename, access_modes[a1], sharing_modes[s1],
1844 NULL, OPEN_EXISTING, 0, 0 );
1845 if (h == INVALID_HANDLE_VALUE)
1846 {
1847 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1848 return;
1849 }
1850 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1851 {
1852 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1853 {
1854 SetLastError(0xdeadbeef);
1855 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1856 NULL, OPEN_EXISTING, 0, 0 );
1857 ret = GetLastError();
1858 if (is_sharing_compatible( access_modes[a1], sharing_modes[s1],
1859 access_modes[a2], sharing_modes[s2] ))
1860 {
1861 ok( h2 != INVALID_HANDLE_VALUE,
1862 "open failed for modes %x/%x/%x/%x\n",
1863 access_modes[a1], sharing_modes[s1],
1864 access_modes[a2], sharing_modes[s2] );
1865 ok( ret == 0, "wrong error code %d\n", ret );
1866 }
1867 else
1868 {
1869 ok( h2 == INVALID_HANDLE_VALUE,
1870 "open succeeded for modes %x/%x/%x/%x\n",
1871 access_modes[a1], sharing_modes[s1],
1872 access_modes[a2], sharing_modes[s2] );
1873 ok( ret == ERROR_SHARING_VIOLATION,
1874 "wrong error code %d\n", ret );
1875 }
1876 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1877 }
1878 }
1879 CloseHandle( h );
1880 }
1881 }
1882
1883 for (a1 = 0; a1 < sizeof(mapping_modes)/sizeof(mapping_modes[0]); a1++)
1884 {
1885 HANDLE m;
1886
1887 create_fake_dll( filename );
1888 SetLastError(0xdeadbeef);
1889 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
1890 if (h == INVALID_HANDLE_VALUE)
1891 {
1892 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
1893 return;
1894 }
1895 m = CreateFileMappingA( h, NULL, mapping_modes[a1], 0, 0, NULL );
1896 ok( m != 0, "failed to create mapping %x err %u\n", mapping_modes[a1], GetLastError() );
1897 CloseHandle( h );
1898 if (!m) continue;
1899
1900 for (a2 = 0; a2 < sizeof(access_modes)/sizeof(access_modes[0]); a2++)
1901 {
1902 for (s2 = 0; s2 < sizeof(sharing_modes)/sizeof(sharing_modes[0]); s2++)
1903 {
1904 SetLastError(0xdeadbeef);
1905 h2 = CreateFileA( filename, access_modes[a2], sharing_modes[s2],
1906 NULL, OPEN_EXISTING, 0, 0 );
1907
1908 ret = GetLastError();
1909 if (h2 == INVALID_HANDLE_VALUE)
1910 {
1911 ok( !is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]),
1912 "open failed for modes map %x/%x/%x\n",
1913 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
1914 ok( ret == ERROR_SHARING_VIOLATION,
1915 "wrong error code %d\n", ret );
1916 }
1917 else
1918 {
1919 if (!is_sharing_map_compatible(mapping_modes[a1], access_modes[a2], sharing_modes[s2]))
1920 ok( broken(1), /* no checking on nt4 */
1921 "open succeeded for modes map %x/%x/%x\n",
1922 mapping_modes[a1], access_modes[a2], sharing_modes[s2] );
1923 ok( ret == 0xdeadbeef /* Win9x */ ||
1924 ret == 0, /* XP */
1925 "wrong error code %d\n", ret );
1926 CloseHandle( h2 );
1927 }
1928 }
1929 }
1930
1931 /* try CREATE_ALWAYS over an existing mapping */
1932 SetLastError(0xdeadbeef);
1933 h2 = CreateFileA( filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
1934 NULL, CREATE_ALWAYS, 0, 0 );
1935 ret = GetLastError();
1936 if (mapping_modes[a1] & SEC_IMAGE)
1937 {
1938 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1939 ok( ret == ERROR_SHARING_VIOLATION, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1940 }
1941 else
1942 {
1943 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1944 ok( ret == ERROR_USER_MAPPED_FILE, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1945 }
1946 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1947
1948 /* try DELETE_ON_CLOSE over an existing mapping */
1949 SetLastError(0xdeadbeef);
1950 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1951 NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 );
1952 ret = GetLastError();
1953 if (mapping_modes[a1] & SEC_IMAGE)
1954 {
1955 ok( h2 == INVALID_HANDLE_VALUE, "create succeeded for map %x\n", mapping_modes[a1] );
1956 ok( ret == ERROR_ACCESS_DENIED, "wrong error code %d for %x\n", ret, mapping_modes[a1] );
1957 }
1958 else
1959 {
1960 ok( h2 != INVALID_HANDLE_VALUE, "open failed for map %x err %u\n", mapping_modes[a1], ret );
1961 }
1962 if (h2 != INVALID_HANDLE_VALUE) CloseHandle( h2 );
1963
1964 CloseHandle( m );
1965 }
1966
1967 SetLastError(0xdeadbeef);
1968 h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, 0 );
1969 ok( h != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1970
1971 SetLastError(0xdeadbeef);
1972 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
1973 ok( h2 == INVALID_HANDLE_VALUE, "CreateFileA should fail\n");
1974 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error code %d\n", GetLastError() );
1975
1976 h2 = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 );
1977 ok( h2 != INVALID_HANDLE_VALUE, "CreateFileA error %d\n", GetLastError() );
1978
1979 CloseHandle(h);
1980 CloseHandle(h2);
1981
1982 DeleteFileA( filename );
1983 }
1984
1985 static char get_windows_drive(void)
1986 {
1987 char windowsdir[MAX_PATH];
1988 GetWindowsDirectory(windowsdir, sizeof(windowsdir));
1989 return windowsdir[0];
1990 }
1991
1992 static void test_FindFirstFileA(void)
1993 {
1994 HANDLE handle;
1995 WIN32_FIND_DATAA data;
1996 int err;
1997 char buffer[5] = "C:\\";
1998 char buffer2[100];
1999 char nonexistent[MAX_PATH];
2000
2001 /* try FindFirstFileA on "C:\" */
2002 buffer[0] = get_windows_drive();
2003
2004 SetLastError( 0xdeadbeaf );
2005 handle = FindFirstFileA(buffer, &data);
2006 err = GetLastError();
2007 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on root directory should fail\n" );
2008 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2009
2010 /* try FindFirstFileA on "C:\*" */
2011 strcpy(buffer2, buffer);
2012 strcat(buffer2, "*");
2013 handle = FindFirstFileA(buffer2, &data);
2014 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2015 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2016 "FindFirstFile shouldn't return '%s' in drive root\n", data.cFileName );
2017 if (FindNextFileA( handle, &data ))
2018 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2019 "FindNextFile shouldn't return '%s' in drive root\n", data.cFileName );
2020 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2021
2022 /* try FindFirstFileA on windows dir */
2023 GetWindowsDirectory( buffer2, sizeof(buffer2) );
2024 strcat(buffer2, "\\*");
2025 handle = FindFirstFileA(buffer2, &data);
2026 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s should succeed\n", buffer2 );
2027 ok( !strcmp( data.cFileName, "." ), "FindFirstFile should return '.' first\n" );
2028 ok( FindNextFileA( handle, &data ), "FindNextFile failed\n" );
2029 ok( !strcmp( data.cFileName, ".." ), "FindNextFile should return '..' as second entry\n" );
2030 while (FindNextFileA( handle, &data ))
2031 ok ( strcmp( data.cFileName, "." ) && strcmp( data.cFileName, ".." ),
2032 "FindNextFile shouldn't return '%s'\n", data.cFileName );
2033 ok ( FindClose(handle) == TRUE, "Failed to close handle %s\n", buffer2 );
2034
2035 /* try FindFirstFileA on "C:\foo\" */
2036 SetLastError( 0xdeadbeaf );
2037 if (!GetTempFileNameA( buffer, "foo", 0, nonexistent ) && GetLastError() == ERROR_ACCESS_DENIED)
2038 {
2039 char tmp[MAX_PATH];
2040 GetTempPathA( sizeof(tmp), tmp );
2041 GetTempFileNameA( tmp, "foo", 0, nonexistent );
2042 }
2043 DeleteFileA( nonexistent );
2044 strcpy(buffer2, nonexistent);
2045 strcat(buffer2, "\\");
2046 handle = FindFirstFileA(buffer2, &data);
2047 err = GetLastError();
2048 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2049 todo_wine {
2050 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2051 }
2052
2053 /* try FindFirstFileA on "C:\foo\bar.txt" */
2054 SetLastError( 0xdeadbeaf );
2055 strcpy(buffer2, nonexistent);
2056 strcat(buffer2, "\\bar.txt");
2057 handle = FindFirstFileA(buffer2, &data);
2058 err = GetLastError();
2059 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2060 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2061
2062 /* try FindFirstFileA on "C:\foo\*.*" */
2063 SetLastError( 0xdeadbeaf );
2064 strcpy(buffer2, nonexistent);
2065 strcat(buffer2, "\\*.*");
2066 handle = FindFirstFileA(buffer2, &data);
2067 err = GetLastError();
2068 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2069 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2070
2071 /* try FindFirstFileA on "foo\bar.txt" */
2072 SetLastError( 0xdeadbeaf );
2073 strcpy(buffer2, nonexistent + 3);
2074 strcat(buffer2, "\\bar.txt");
2075 handle = FindFirstFileA(buffer2, &data);
2076 err = GetLastError();
2077 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2078 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2079
2080 /* try FindFirstFileA on "c:\nul" */
2081 SetLastError( 0xdeadbeaf );
2082 strcpy(buffer2, buffer);
2083 strcat(buffer2, "nul");
2084 handle = FindFirstFileA(buffer2, &data);
2085 err = GetLastError();
2086 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2087 ok( 0 == lstrcmpiA(data.cFileName, "nul"), "wrong name %s\n", data.cFileName );
2088 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2089 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2090 "wrong attributes %x\n", data.dwFileAttributes );
2091 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2092 {
2093 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2094 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2095 }
2096 SetLastError( 0xdeadbeaf );
2097 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2098 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2099 ok( FindClose( handle ), "failed to close handle\n" );
2100
2101 /* try FindFirstFileA on "lpt1" */
2102 SetLastError( 0xdeadbeaf );
2103 strcpy(buffer2, "lpt1");
2104 handle = FindFirstFileA(buffer2, &data);
2105 err = GetLastError();
2106 ok( handle != INVALID_HANDLE_VALUE, "FindFirstFile on %s failed: %d\n", buffer2, err );
2107 ok( 0 == lstrcmpiA(data.cFileName, "lpt1"), "wrong name %s\n", data.cFileName );
2108 ok( FILE_ATTRIBUTE_ARCHIVE == data.dwFileAttributes ||
2109 FILE_ATTRIBUTE_DEVICE == data.dwFileAttributes /* Win9x */,
2110 "wrong attributes %x\n", data.dwFileAttributes );
2111 if (data.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
2112 {
2113 ok( 0 == data.nFileSizeHigh, "wrong size %d\n", data.nFileSizeHigh );
2114 ok( 0 == data.nFileSizeLow, "wrong size %d\n", data.nFileSizeLow );
2115 }
2116 SetLastError( 0xdeadbeaf );
2117 ok( !FindNextFileA( handle, &data ), "FindNextFileA succeeded\n" );
2118 ok( GetLastError() == ERROR_NO_MORE_FILES, "bad error %d\n", GetLastError() );
2119 ok( FindClose( handle ), "failed to close handle\n" );
2120
2121 /* try FindFirstFileA on "c:\nul\*" */
2122 SetLastError( 0xdeadbeaf );
2123 strcpy(buffer2, buffer);
2124 strcat(buffer2, "nul\\*");
2125 handle = FindFirstFileA(buffer2, &data);
2126 err = GetLastError();
2127 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2128 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2129
2130 /* try FindFirstFileA on "c:\nul*" */
2131 SetLastError( 0xdeadbeaf );
2132 strcpy(buffer2, buffer);
2133 strcat(buffer2, "nul*");
2134 handle = FindFirstFileA(buffer2, &data);
2135 err = GetLastError();
2136 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2137 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err );
2138
2139 /* try FindFirstFileA on "c:\foo\bar\nul" */
2140 SetLastError( 0xdeadbeaf );
2141 strcpy(buffer2, buffer);
2142 strcat(buffer2, "foo\\bar\\nul");
2143 handle = FindFirstFileA(buffer2, &data);
2144 err = GetLastError();
2145 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2146 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2147
2148 /* try FindFirstFileA on "c:\foo\nul\bar" */
2149 SetLastError( 0xdeadbeaf );
2150 strcpy(buffer2, buffer);
2151 strcat(buffer2, "foo\\nul\\bar");
2152 handle = FindFirstFileA(buffer2, &data);
2153 err = GetLastError();
2154 ok ( handle == INVALID_HANDLE_VALUE, "FindFirstFile on %s should Fail\n", buffer2 );
2155 ok ( err == ERROR_PATH_NOT_FOUND, "Bad Error number %d\n", err );
2156 }
2157
2158 static void test_FindNextFileA(void)
2159 {
2160 HANDLE handle;
2161 WIN32_FIND_DATAA search_results;
2162 int err;
2163 char buffer[5] = "C:\\*";
2164
2165 buffer[0] = get_windows_drive();
2166 handle = FindFirstFileA(buffer,&search_results);
2167 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed\n" );
2168 while (FindNextFile(handle, &search_results))
2169 {
2170 /* get to the end of the files */
2171 }
2172 ok ( FindClose(handle) == TRUE, "Failed to close handle\n");
2173 err = GetLastError();
2174 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES\n");
2175 }
2176
2177 static void test_FindFirstFileExA(FINDEX_SEARCH_OPS search_ops)
2178 {
2179 WIN32_FIND_DATAA search_results;
2180 HANDLE handle;
2181 BOOL ret;
2182
2183 if (!pFindFirstFileExA)
2184 {
2185 win_skip("FindFirstFileExA() is missing\n");
2186 return;
2187 }
2188
2189 CreateDirectoryA("test-dir", NULL);
2190 _lclose(_lcreat("test-dir\\file1", 0));
2191 _lclose(_lcreat("test-dir\\file2", 0));
2192 CreateDirectoryA("test-dir\\dir1", NULL);
2193 SetLastError(0xdeadbeef);
2194 handle = pFindFirstFileExA("test-dir\\*", FindExInfoStandard, &search_results, search_ops, NULL, 0);
2195 if (handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2196 {
2197 win_skip("FindFirstFileExA is not implemented\n");
2198 goto cleanup;
2199 }
2200 ok(handle != INVALID_HANDLE_VALUE, "FindFirstFile failed (err=%u)\n", GetLastError());
2201 ok(strcmp(search_results.cFileName, ".") == 0, "First entry should be '.', is %s\n", search_results.cFileName);
2202
2203 #define CHECK_NAME(fn) (strcmp((fn), "file1") == 0 || strcmp((fn), "file2") == 0 || strcmp((fn), "dir1") == 0)
2204
2205 ok(FindNextFile(handle, &search_results), "Fetching second file failed\n");
2206 ok(strcmp(search_results.cFileName, "..") == 0, "Second entry should be '..' is %s\n", search_results.cFileName);
2207
2208 ok(FindNextFile(handle, &search_results), "Fetching third file failed\n");
2209 ok(CHECK_NAME(search_results.cFileName), "Invalid third entry - %s\n", search_results.cFileName);
2210
2211 SetLastError(0xdeadbeef);
2212 ret = FindNextFile(handle, &search_results);
2213 if (!ret && (GetLastError() == ERROR_NO_MORE_FILES) && (search_ops == FindExSearchLimitToDirectories))
2214 {
2215 skip("File system supports directory filtering\n");
2216 /* Results from the previous call are not cleared */
2217 ok(strcmp(search_results.cFileName, "dir1") == 0, "Third entry should be 'dir1' is %s\n", search_results.cFileName);
2218 FindClose( handle );
2219 goto cleanup;
2220 }
2221
2222 ok(ret, "Fetching fourth file failed\n");
2223 ok(CHECK_NAME(search_results.cFileName), "Invalid fourth entry - %s\n", search_results.cFileName);
2224
2225 ok(FindNextFile(handle, &search_results), "Fetching fifth file failed\n");
2226 ok(CHECK_NAME(search_results.cFileName), "Invalid fifth entry - %s\n", search_results.cFileName);
2227
2228 #undef CHECK_NAME
2229
2230 ok(FindNextFile(handle, &search_results) == FALSE, "Fetching sixth file should fail\n");
2231
2232 FindClose( handle );
2233
2234 cleanup:
2235 DeleteFileA("test-dir\\file1");
2236 DeleteFileA("test-dir\\file2");
2237 RemoveDirectoryA("test-dir\\dir1");
2238 RemoveDirectoryA("test-dir");
2239 }
2240
2241 static int test_Mapfile_createtemp(HANDLE *handle)
2242 {
2243 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
2244 DeleteFile(filename);
2245 *handle = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
2246 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2247 if (*handle != INVALID_HANDLE_VALUE) {
2248
2249 return 1;
2250 }
2251
2252 return 0;
2253 }
2254
2255 static void test_MapFile(void)
2256 {
2257 HANDLE handle;
2258 HANDLE hmap;
2259
2260 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2261
2262 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, "named_file_map" );
2263 ok( hmap != NULL, "mapping should work, I named it!\n" );
2264
2265 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2266
2267 /* We have to close file before we try new stuff with mapping again.
2268 Else we would always succeed on XP or block descriptors on 95. */
2269 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2270 ok( hmap != NULL, "We should still be able to map!\n" );
2271 ok( CloseHandle( hmap ), "can't close mapping handle\n");
2272 ok( CloseHandle( handle ), "can't close file handle\n");
2273 handle = NULL;
2274
2275 ok(test_Mapfile_createtemp(&handle), "Couldn't create test file.\n");
2276
2277 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
2278 ok( hmap == NULL, "mapped zero size file\n");
2279 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
2280
2281 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0, NULL );
2282 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2283 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2284 if ( hmap )
2285 CloseHandle( hmap );
2286
2287 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x80000000, 0x10000, NULL );
2288 ok( hmap == NULL || broken(hmap != NULL) /* NT4 */, "mapping should fail\n");
2289 /* GetLastError() varies between win9x and WinNT and also depends on the filesystem */
2290 if ( hmap )
2291 CloseHandle( hmap );
2292
2293 /* On XP you can now map again, on Win 95 you cannot. */
2294
2295 ok( CloseHandle( handle ), "can't close file handle\n");
2296 ok( DeleteFileA( filename ), "DeleteFile failed after map\n" );
2297 }
2298
2299 static void test_GetFileType(void)
2300 {
2301 DWORD type;
2302 HANDLE h = CreateFileA( filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
2303 ok( h != INVALID_HANDLE_VALUE, "open %s failed\n", filename );
2304 type = GetFileType(h);
2305 ok( type == FILE_TYPE_DISK, "expected type disk got %d\n", type );
2306 CloseHandle( h );
2307 h = CreateFileA( "nul", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2308 ok( h != INVALID_HANDLE_VALUE, "open nul failed\n" );
2309 type = GetFileType(h);
2310 ok( type == FILE_TYPE_CHAR, "expected type char for nul got %d\n", type );
2311 CloseHandle( h );
2312 DeleteFileA( filename );
2313 }
2314
2315 static int completion_count;
2316
2317 static void CALLBACK FileIOComplete(DWORD dwError, DWORD dwBytes, LPOVERLAPPED ovl)
2318 {
2319 /* printf("(%ld, %ld, %p { %ld, %ld, %ld, %ld, %p })\n", dwError, dwBytes, ovl, ovl->Internal, ovl->InternalHigh, ovl->Offset, ovl->OffsetHigh, ovl->hEvent);*/
2320 ReleaseSemaphore(ovl->hEvent, 1, NULL);
2321 completion_count++;
2322 }
2323
2324 static void test_async_file_errors(void)
2325 {
2326 char szFile[MAX_PATH];
2327 HANDLE hSem = CreateSemaphoreW(NULL, 1, 1, NULL);
2328 HANDLE hFile;
2329 LPVOID lpBuffer = HeapAlloc(GetProcessHeap(), 0, 4096);
2330 OVERLAPPED ovl;
2331 S(U(ovl)).Offset = 0;
2332 S(U(ovl)).OffsetHigh = 0;
2333 ovl.hEvent = hSem;
2334 completion_count = 0;
2335 szFile[0] = '\0';
2336 GetWindowsDirectoryA(szFile, sizeof(szFile)/sizeof(szFile[0])-1-strlen("\\win.ini"));
2337 strcat(szFile, "\\win.ini");
2338 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2339 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2340 if (hFile == INVALID_HANDLE_VALUE) /* win9x doesn't like FILE_SHARE_DELETE */
2341 hFile = CreateFileA(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
2342 NULL, OPEN_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
2343 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA(%s ...) failed\n", szFile);
2344 while (TRUE)
2345 {
2346 BOOL res;
2347 DWORD count;
2348 while (WaitForSingleObjectEx(hSem, INFINITE, TRUE) == WAIT_IO_COMPLETION)
2349 ;
2350 res = ReadFileEx(hFile, lpBuffer, 4096, &ovl, FileIOComplete);
2351 /*printf("Offset = %ld, result = %s\n", ovl.Offset, res ? "TRUE" : "FALSE");*/
2352 if (!res)
2353 break;
2354 if (!GetOverlappedResult(hFile, &ovl, &count, FALSE))
2355 break;
2356 S(U(ovl)).Offset += count;
2357 /* i/o completion routine only called if ReadFileEx returned success.
2358 * we only care about violations of this rule so undo what should have
2359 * been done */
2360 completion_count--;
2361 }
2362 ok(completion_count == 0, "completion routine should only be called when ReadFileEx succeeds (this rule was violated %d times)\n", completion_count);
2363 /*printf("Error = %ld\n", GetLastError());*/
2364 HeapFree(GetProcessHeap(), 0, lpBuffer);
2365 }
2366
2367 static BOOL user_apc_ran;
2368 static void CALLBACK user_apc(ULONG_PTR param)
2369 {
2370 user_apc_ran = TRUE;
2371 }
2372
2373 static void test_read_write(void)
2374 {
2375 DWORD bytes, ret, old_prot;
2376 HANDLE hFile;
2377 char temp_path[MAX_PATH];
2378 char filename[MAX_PATH];
2379 char *mem;
2380 static const char prefix[] = "pfx";
2381
2382 ret = GetTempPathA(MAX_PATH, temp_path);
2383 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2384 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2385
2386 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
2387 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
2388
2389 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2390 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
2391 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
2392
2393 user_apc_ran = FALSE;
2394 if (pQueueUserAPC) {
2395 trace("Queueing an user APC\n"); /* verify the file is non alerable */
2396 ok(pQueueUserAPC(&user_apc, GetCurrentThread(), 0), "QueueUserAPC failed: %d\n", GetLastError());
2397 }
2398
2399 SetLastError(12345678);
2400 bytes = 12345678;
2401 ret = WriteFile(hFile, NULL, 0, &bytes, NULL);
2402 ok(ret && GetLastError() == 12345678,
2403 "ret = %d, error %d\n", ret, GetLastError());
2404 ok(!bytes, "bytes = %d\n", bytes);
2405
2406 SetLastError(12345678);
2407 bytes = 12345678;
2408 ret = WriteFile(hFile, NULL, 10, &bytes, NULL);
2409 ok((!ret && GetLastError() == ERROR_INVALID_USER_BUFFER) || /* Win2k */
2410 (ret && GetLastError() == 12345678), /* Win9x */
2411 "ret = %d, error %d\n", ret, GetLastError());
2412 ok(!bytes || /* Win2k */
2413 bytes == 10, /* Win9x */
2414 "bytes = %d\n", bytes);
2415
2416 /* make sure the file contains data */
2417 WriteFile(hFile, "this is the test data", 21, &bytes, NULL);
2418 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
2419
2420 SetLastError(12345678);
2421 bytes = 12345678;
2422 ret = ReadFile(hFile, NULL, 0, &bytes, NULL);
2423 ok(ret && GetLastError() == 12345678,
2424 "ret = %d, error %d\n", ret, GetLastError());
2425 ok(!bytes, "bytes = %d\n", bytes);
2426
2427 SetLastError(12345678);
2428 bytes = 12345678;
2429 ret = ReadFile(hFile, NULL, 10, &bytes, NULL);
2430 ok(!ret && (GetLastError() == ERROR_NOACCESS || /* Win2k */
2431 GetLastError() == ERROR_INVALID_PARAMETER), /* Win9x */
2432 "ret = %d, error %d\n", ret, GetLastError());
2433 ok(!bytes, "bytes = %d\n", bytes);
2434
2435 ok(user_apc_ran == FALSE, "UserAPC ran, file using alertable io mode\n");
2436 if (pQueueUserAPC)
2437 SleepEx(0, TRUE); /* get rid of apc */
2438
2439 /* test passing protected memory as buffer */
2440
2441 mem = VirtualAlloc( NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE );
2442 ok( mem != NULL, "failed to allocate virtual mem error %u\n", GetLastError() );
2443
2444 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2445 ok( ret, "WriteFile failed error %u\n", GetLastError() );
2446 ok( bytes == 0x4000, "only wrote %x bytes\n", bytes );
2447
2448 ret = VirtualProtect( mem + 0x2000, 0x2000, PAGE_NOACCESS, &old_prot );
2449 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2450
2451 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2452 ok( !ret, "WriteFile succeeded\n" );
2453 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2454 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2455 "wrong error %u\n", GetLastError() );
2456 ok( bytes == 0, "wrote %x bytes\n", bytes );
2457
2458 ret = WriteFile( (HANDLE)0xdead, mem, 0x4000, &bytes, NULL );
2459 ok( !ret, "WriteFile succeeded\n" );
2460 ok( GetLastError() == ERROR_INVALID_HANDLE || /* handle is checked before buffer on NT */
2461 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2462 "wrong error %u\n", GetLastError() );
2463 ok( bytes == 0, "wrote %x bytes\n", bytes );
2464
2465 ret = VirtualProtect( mem, 0x2000, PAGE_NOACCESS, &old_prot );
2466 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2467
2468 ret = WriteFile( hFile, mem, 0x4000, &bytes, NULL );
2469 ok( !ret, "WriteFile succeeded\n" );
2470 ok( GetLastError() == ERROR_INVALID_USER_BUFFER ||
2471 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2472 "wrong error %u\n", GetLastError() );
2473 ok( bytes == 0, "wrote %x bytes\n", bytes );
2474
2475 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2476
2477 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2478 ok( !ret, "ReadFile succeeded\n" );
2479 ok( GetLastError() == ERROR_NOACCESS ||
2480 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2481 "wrong error %u\n", GetLastError() );
2482 ok( bytes == 0, "read %x bytes\n", bytes );
2483
2484 ret = VirtualProtect( mem, 0x2000, PAGE_READONLY, &old_prot );
2485 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2486
2487 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2488 ok( !ret, "ReadFile succeeded\n" );
2489 ok( GetLastError() == ERROR_NOACCESS ||
2490 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2491 "wrong error %u\n", GetLastError() );
2492 ok( bytes == 0, "read %x bytes\n", bytes );
2493
2494 ret = VirtualProtect( mem, 0x2000, PAGE_READWRITE, &old_prot );
2495 ok( ret, "VirtualProtect failed error %u\n", GetLastError() );
2496
2497 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2498 ok( !ret, "ReadFile succeeded\n" );
2499 ok( GetLastError() == ERROR_NOACCESS ||
2500 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2501 "wrong error %u\n", GetLastError() );
2502 ok( bytes == 0, "read %x bytes\n", bytes );
2503
2504 SetFilePointer( hFile, 0x1234, NULL, FILE_BEGIN );
2505 SetEndOfFile( hFile );
2506 SetFilePointer( hFile, 0, NULL, FILE_BEGIN );
2507
2508 ret = ReadFile( hFile, mem, 0x4000, &bytes, NULL );
2509 ok( !ret, "ReadFile succeeded\n" );
2510 ok( GetLastError() == ERROR_NOACCESS ||
2511 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2512 "wrong error %u\n", GetLastError() );
2513 ok( bytes == 0, "read %x bytes\n", bytes );
2514
2515 ret = ReadFile( hFile, mem, 0x2000, &bytes, NULL );
2516 ok( ret, "ReadFile failed error %u\n", GetLastError() );
2517 ok( bytes == 0x1234, "read %x bytes\n", bytes );
2518
2519 ret = ReadFile( hFile, NULL, 1, &bytes, NULL );
2520 ok( !ret, "ReadFile succeeded\n" );
2521 ok( GetLastError() == ERROR_NOACCESS ||
2522 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
2523 "wrong error %u\n", GetLastError() );
2524 ok( bytes == 0, "read %x bytes\n", bytes );
2525
2526 VirtualFree( mem, 0, MEM_FREE );
2527
2528 ret = CloseHandle(hFile);
2529 ok( ret, "CloseHandle: error %d\n", GetLastError());
2530 ret = DeleteFileA(filename);
2531 ok( ret, "DeleteFileA: error %d\n", GetLastError());
2532 }
2533
2534 static void test_OpenFile(void)
2535 {
2536 HFILE hFile;
2537 OFSTRUCT ofs;
2538 BOOL ret;
2539 DWORD retval;
2540
2541 static const char file[] = "regedit.exe";
2542 static const char foo[] = ".\\foo-bar-foo.baz";
2543 static const char *foo_too_long = ".\\foo-bar-foo.baz+++++++++++++++"
2544 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2545 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2546 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2547 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
2548 "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2549 char buff[MAX_PATH];
2550 char buff_long[4*MAX_PATH];
2551 char filled_0xA5[OFS_MAXPATHNAME];
2552 char *p;
2553 UINT length;
2554
2555 /* Check for existing file */
2556 if (!pGetSystemWindowsDirectoryA)
2557 length = GetWindowsDirectoryA(buff, MAX_PATH);
2558 else
2559 length = pGetSystemWindowsDirectoryA(buff, MAX_PATH);
2560
2561 if (length + sizeof(file) < MAX_PATH)
2562 {
2563 p = buff + strlen(buff);
2564 if (p > buff && p[-1] != '\\') *p++ = '\\';
2565 strcpy( p, file );
2566 memset(&ofs, 0xA5, sizeof(ofs));
2567 SetLastError(0xfaceabee);
2568
2569 hFile = OpenFile(buff, &ofs, OF_EXIST);
2570 ok( hFile == TRUE, "%s not found : %d\n", buff, GetLastError() );
2571 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2572 "GetLastError() returns %d\n", GetLastError() );
2573 ok( ofs.cBytes == sizeof(ofs), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2574 ok( ofs.nErrCode == ERROR_SUCCESS, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2575 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2576 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2577 ofs.szPathName, buff );
2578 }
2579
2580 memset(&filled_0xA5, 0xA5, OFS_MAXPATHNAME);
2581 length = GetCurrentDirectoryA(MAX_PATH, buff);
2582
2583 /* Check for nonexistent file */
2584 if (length + sizeof(foo) < MAX_PATH)
2585 {
2586 p = buff + strlen(buff);
2587 if (p > buff && p[-1] != '\\') *p++ = '\\';
2588 strcpy( p, foo + 2 );
2589 memset(&ofs, 0xA5, sizeof(ofs));
2590 SetLastError(0xfaceabee);
2591
2592 hFile = OpenFile(foo, &ofs, OF_EXIST);
2593 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2594 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() returns %d\n", GetLastError() );
2595 todo_wine
2596 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2597 ok( ofs.nErrCode == ERROR_FILE_NOT_FOUND, "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2598 ok( lstrcmpiA(ofs.szPathName, buff) == 0 || strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2599 "OpenFile returned '%s', but was expected to return '%s' or string filled with 0xA5\n",
2600 ofs.szPathName, buff );
2601 }
2602
2603 length = GetCurrentDirectoryA(MAX_PATH, buff_long);
2604 length += lstrlenA(foo_too_long + 1);
2605
2606 /* Check for nonexistent file with too long filename */
2607 if (length >= OFS_MAXPATHNAME && length < sizeof(buff_long))
2608 {
2609 lstrcatA(buff_long, foo_too_long + 1); /* Avoid '.' during concatenation */
2610 memset(&ofs, 0xA5, sizeof(ofs));
2611 SetLastError(0xfaceabee);
2612
2613 hFile = OpenFile(foo_too_long, &ofs, OF_EXIST);
2614 ok( hFile == HFILE_ERROR, "hFile != HFILE_ERROR : %d\n", GetLastError());
2615 ok( GetLastError() == ERROR_INVALID_DATA || GetLastError() == ERROR_FILENAME_EXCED_RANGE,
2616 "GetLastError() returns %d\n", GetLastError() );
2617 todo_wine
2618 ok( ofs.cBytes == 0xA5, "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2619 ok( ofs.nErrCode == ERROR_INVALID_DATA || ofs.nErrCode == ERROR_FILENAME_EXCED_RANGE,
2620 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2621 ok( strncmp(ofs.szPathName, filled_0xA5, OFS_MAXPATHNAME) == 0,
2622 "OpenFile returned '%s', but was expected to return string filled with 0xA5\n",
2623 ofs.szPathName );
2624 }
2625
2626 length = GetCurrentDirectoryA(MAX_PATH, buff) + sizeof(filename);
2627
2628 if (length >= MAX_PATH)
2629 {
2630 trace("Buffer too small, requested length = %d, but MAX_PATH = %d. Skipping test.\n", length, MAX_PATH);
2631 return;
2632 }
2633 p = buff + strlen(buff);
2634 if (p > buff && p[-1] != '\\') *p++ = '\\';
2635 strcpy( p, filename );
2636
2637 memset(&ofs, 0xA5, sizeof(ofs));
2638 SetLastError(0xfaceabee);
2639 /* Create an empty file */
2640 hFile = OpenFile(filename, &ofs, OF_CREATE);
2641 ok( hFile != HFILE_ERROR, "OpenFile failed to create nonexistent file\n" );
2642 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2643 "GetLastError() returns %d\n", GetLastError() );
2644 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2645 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2646 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2647 ret = _lclose(hFile);
2648 ok( !ret, "_lclose() returns %d\n", ret );
2649 retval = GetFileAttributesA(filename);
2650 ok( retval != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA: error %d\n", GetLastError() );
2651
2652 memset(&ofs, 0xA5, sizeof(ofs));
2653 SetLastError(0xfaceabee);
2654 /* Check various opening options: */
2655 /* for reading only, */
2656 hFile = OpenFile(filename, &ofs, OF_READ);
2657 ok( hFile != HFILE_ERROR, "OpenFile failed on read\n" );
2658 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2659 "GetLastError() returns %d\n", GetLastError() );
2660 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2661 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2662 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2663 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2664 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2665 ret = _lclose(hFile);
2666 ok( !ret, "_lclose() returns %d\n", ret );
2667
2668 memset(&ofs, 0xA5, sizeof(ofs));
2669 SetLastError(0xfaceabee);
2670 /* for writing only, */
2671 hFile = OpenFile(filename, &ofs, OF_WRITE);
2672 ok( hFile != HFILE_ERROR, "OpenFile failed on write\n" );
2673 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2674 "GetLastError() returns %d\n", GetLastError() );
2675 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2676 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2677 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2678 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2679 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2680 ret = _lclose(hFile);
2681 ok( !ret, "_lclose() returns %d\n", ret );
2682
2683 memset(&ofs, 0xA5, sizeof(ofs));
2684 SetLastError(0xfaceabee);
2685 /* for reading and writing, */
2686 hFile = OpenFile(filename, &ofs, OF_READWRITE);
2687 ok( hFile != HFILE_ERROR, "OpenFile failed on read/write\n" );
2688 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2689 "GetLastError() returns %d\n", GetLastError() );
2690 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2691 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2692 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2693 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2694 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2695 ret = _lclose(hFile);
2696 ok( !ret, "_lclose() returns %d\n", ret );
2697
2698 memset(&ofs, 0xA5, sizeof(ofs));
2699 SetLastError(0xfaceabee);
2700 /* for checking file presence. */
2701 hFile = OpenFile(filename, &ofs, OF_EXIST);
2702 ok( hFile == 1, "OpenFile failed on finding our created file\n" );
2703 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2704 "GetLastError() returns %d\n", GetLastError() );
2705 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2706 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2707 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2708 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2709 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2710
2711 memset(&ofs, 0xA5, sizeof(ofs));
2712 SetLastError(0xfaceabee);
2713 /* Delete the file and make sure it doesn't exist anymore */
2714 hFile = OpenFile(filename, &ofs, OF_DELETE);
2715 ok( hFile == 1, "OpenFile failed on delete (%d)\n", hFile );
2716 ok( GetLastError() == 0xfaceabee || GetLastError() == ERROR_SUCCESS,
2717 "GetLastError() returns %d\n", GetLastError() );
2718 ok( ofs.cBytes == sizeof(OFSTRUCT), "OpenFile set ofs.cBytes to %d\n", ofs.cBytes );
2719 ok( ofs.nErrCode == ERROR_SUCCESS || broken(ofs.nErrCode != ERROR_SUCCESS) /* win9x */,
2720 "OpenFile set ofs.nErrCode to %d\n", ofs.nErrCode );
2721 ok( lstrcmpiA(ofs.szPathName, buff) == 0,
2722 "OpenFile returned '%s', but was expected to return '%s'\n", ofs.szPathName, buff );
2723
2724 retval = GetFileAttributesA(filename);
2725 ok( retval == INVALID_FILE_ATTRIBUTES, "GetFileAttributesA succeeded on deleted file\n" );
2726 }
2727
2728 static void test_overlapped(void)
2729 {
2730 OVERLAPPED ov;
2731 DWORD r, result;
2732
2733 /* GetOverlappedResult crashes if the 2nd or 3rd param are NULL */
2734 if (0) /* tested: WinXP */
2735 {
2736 GetOverlappedResult(0, NULL, &result, FALSE);
2737 GetOverlappedResult(0, &ov, NULL, FALSE);
2738 GetOverlappedResult(0, NULL, NULL, FALSE);
2739 }
2740
2741 memset( &ov, 0, sizeof ov );
2742 result = 1;
2743 r = GetOverlappedResult(0, &ov, &result, 0);
2744 if (r)
2745 ok( result == 0, "wrong result %u\n", result );
2746 else /* win9x */
2747 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2748
2749 result = 0;
2750 ov.Internal = 0;
2751 ov.InternalHigh = 0xabcd;
2752 r = GetOverlappedResult(0, &ov, &result, 0);
2753 if (r)
2754 ok( result == 0xabcd, "wrong result %u\n", result );
2755 else /* win9x */
2756 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2757
2758 SetLastError( 0xb00 );
2759 result = 0;
2760 ov.Internal = STATUS_INVALID_HANDLE;
2761 ov.InternalHigh = 0xabcd;
2762 r = GetOverlappedResult(0, &ov, &result, 0);
2763 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2764 ok( r == FALSE, "should return false\n");
2765 ok( result == 0xabcd || result == 0 /* win9x */, "wrong result %u\n", result );
2766
2767 SetLastError( 0xb00 );
2768 result = 0;
2769 ov.Internal = STATUS_PENDING;
2770 ov.InternalHigh = 0xabcd;
2771 r = GetOverlappedResult(0, &ov, &result, 0);
2772 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2773 "wrong error %u\n", GetLastError() );
2774 ok( r == FALSE, "should return false\n");
2775 ok( result == 0, "wrong result %u\n", result );
2776
2777 SetLastError( 0xb00 );
2778 ov.hEvent = CreateEvent( NULL, 1, 1, NULL );
2779 ov.Internal = STATUS_PENDING;
2780 ov.InternalHigh = 0xabcd;
2781 r = GetOverlappedResult(0, &ov, &result, 0);
2782 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2783 "wrong error %u\n", GetLastError() );
2784 ok( r == FALSE, "should return false\n");
2785
2786 ResetEvent( ov.hEvent );
2787
2788 SetLastError( 0xb00 );
2789 ov.Internal = STATUS_PENDING;
2790 ov.InternalHigh = 0;
2791 r = GetOverlappedResult(0, &ov, &result, 0);
2792 ok( GetLastError() == ERROR_IO_INCOMPLETE || GetLastError() == ERROR_INVALID_HANDLE /* win9x */,
2793 "wrong error %u\n", GetLastError() );
2794 ok( r == FALSE, "should return false\n");
2795
2796 r = CloseHandle( ov.hEvent );
2797 ok( r == TRUE, "close handle failed\n");
2798 }
2799
2800 static void test_RemoveDirectory(void)
2801 {
2802 int rc;
2803 char directory[] = "removeme";
2804
2805 rc = CreateDirectory(directory, NULL);
2806 ok( rc, "Createdirectory failed, gle=%d\n", GetLastError() );
2807
2808 rc = SetCurrentDirectory(directory);
2809 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2810
2811 rc = RemoveDirectory(".");
2812 if (!rc)
2813 {
2814 rc = SetCurrentDirectory("..");
2815 ok( rc, "SetCurrentDirectory failed, gle=%d\n", GetLastError() );
2816
2817 rc = RemoveDirectory(directory);
2818 ok( rc, "RemoveDirectory failed, gle=%d\n", GetLastError() );
2819 }
2820 }
2821
2822 static BOOL check_file_time( const FILETIME *ft1, const FILETIME *ft2, UINT tolerance )
2823 {
2824 ULONGLONG t1 = ((ULONGLONG)ft1->dwHighDateTime << 32) | ft1->dwLowDateTime;
2825 ULONGLONG t2 = ((ULONGLONG)ft2->dwHighDateTime << 32) | ft2->dwLowDateTime;
2826 return abs(t1 - t2) <= tolerance;
2827 }
2828
2829 static void test_ReplaceFileA(void)
2830 {
2831 char replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
2832 HANDLE hReplacedFile, hReplacementFile, hBackupFile;
2833 static const char replacedData[] = "file-to-replace";
2834 static const char replacementData[] = "new-file";
2835 static const char backupData[] = "backup-file";
2836 FILETIME ftReplaced, ftReplacement, ftBackup;
2837 static const char prefix[] = "pfx";
2838 char temp_path[MAX_PATH];
2839 DWORD ret;
2840 BOOL retok, removeBackup = FALSE;
2841
2842 if (!pReplaceFileA)
2843 {
2844 win_skip("ReplaceFileA() is missing\n");
2845 return;
2846 }
2847
2848 ret = GetTempPathA(MAX_PATH, temp_path);
2849 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
2850 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
2851
2852 ret = GetTempFileNameA(temp_path, prefix, 0, replaced);
2853 ok(ret != 0, "GetTempFileNameA error (replaced) %d\n", GetLastError());
2854
2855 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2856 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2857
2858 ret = GetTempFileNameA(temp_path, prefix, 0, backup);
2859 ok(ret != 0, "GetTempFileNameA error (backup) %d\n", GetLastError());
2860
2861 /* place predictable data in the file to be replaced */
2862 hReplacedFile = CreateFileA(replaced, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2863 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2864 "failed to open replaced file\n");
2865 retok = WriteFile(hReplacedFile, replacedData, sizeof(replacedData), &ret, NULL );
2866 ok( retok && ret == sizeof(replacedData),
2867 "WriteFile error (replaced) %d\n", GetLastError());
2868 ok(GetFileSize(hReplacedFile, NULL) == sizeof(replacedData),
2869 "replaced file has wrong size\n");
2870 /* place predictable data in the file to be the replacement */
2871 hReplacementFile = CreateFileA(replacement, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2872 ok(hReplacementFile != INVALID_HANDLE_VALUE,
2873 "failed to open replacement file\n");
2874 retok = WriteFile(hReplacementFile, replacementData, sizeof(replacementData), &ret, NULL );
2875 ok( retok && ret == sizeof(replacementData),
2876 "WriteFile error (replacement) %d\n", GetLastError());
2877 ok(GetFileSize(hReplacementFile, NULL) == sizeof(replacementData),
2878 "replacement file has wrong size\n");
2879 /* place predictable data in the backup file (to be over-written) */
2880 hBackupFile = CreateFileA(backup, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
2881 ok(hBackupFile != INVALID_HANDLE_VALUE,
2882 "failed to open backup file\n");
2883 retok = WriteFile(hBackupFile, backupData, sizeof(backupData), &ret, NULL );
2884 ok( retok && ret == sizeof(backupData),
2885 "WriteFile error (replacement) %d\n", GetLastError());
2886 ok(GetFileSize(hBackupFile, NULL) == sizeof(backupData),
2887 "backup file has wrong size\n");
2888 /* change the filetime on the "replaced" file to ensure that it changes */
2889 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2890 ok( ret, "GetFileTime error (replaced) %d\n", GetLastError());
2891 ftReplaced.dwLowDateTime -= 600000000; /* 60 second */
2892 ret = SetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2893 ok( ret, "SetFileTime error (replaced) %d\n", GetLastError());
2894 GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced); /* get the actual time back */
2895 CloseHandle(hReplacedFile);
2896 /* change the filetime on the backup to ensure that it changes */
2897 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2898 ok( ret, "GetFileTime error (backup) %d\n", GetLastError());
2899 ftBackup.dwLowDateTime -= 1200000000; /* 120 second */
2900 ret = SetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2901 ok( ret, "SetFileTime error (backup) %d\n", GetLastError());
2902 GetFileTime(hBackupFile, NULL, NULL, &ftBackup); /* get the actual time back */
2903 CloseHandle(hBackupFile);
2904 /* get the filetime on the replacement file to perform checks */
2905 ret = GetFileTime(hReplacementFile, NULL, NULL, &ftReplacement);
2906 ok( ret, "GetFileTime error (replacement) %d\n", GetLastError());
2907 CloseHandle(hReplacementFile);
2908
2909 /* perform replacement w/ backup
2910 * TODO: flags are not implemented
2911 */
2912 SetLastError(0xdeadbeef);
2913 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2914 ok(ret, "ReplaceFileA: unexpected error %d\n", GetLastError());
2915 /* make sure that the backup has the size of the old "replaced" file */
2916 hBackupFile = CreateFileA(backup, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2917 ok(hBackupFile != INVALID_HANDLE_VALUE,
2918 "failed to open backup file\n");
2919 ret = GetFileSize(hBackupFile, NULL);
2920 ok(ret == sizeof(replacedData),
2921 "backup file has wrong size %d\n", ret);
2922 /* make sure that the "replaced" file has the size of the replacement file */
2923 hReplacedFile = CreateFileA(replaced, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2924 ok(hReplacedFile != INVALID_HANDLE_VALUE,
2925 "failed to open replaced file: %d\n", GetLastError());
2926 if (hReplacedFile != INVALID_HANDLE_VALUE)
2927 {
2928 ret = GetFileSize(hReplacedFile, NULL);
2929 ok(ret == sizeof(replacementData),
2930 "replaced file has wrong size %d\n", ret);
2931 /* make sure that the replacement file no-longer exists */
2932 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2933 ok(hReplacementFile == INVALID_HANDLE_VALUE,
2934 "unexpected error, replacement file should not exist %d\n", GetLastError());
2935 /* make sure that the backup has the old "replaced" filetime */
2936 ret = GetFileTime(hBackupFile, NULL, NULL, &ftBackup);
2937 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2938 ok(check_file_time(&ftBackup, &ftReplaced, 20000000), "backup file has wrong filetime\n");
2939 CloseHandle(hBackupFile);
2940 /* make sure that the "replaced" has the old replacement filetime */
2941 ret = GetFileTime(hReplacedFile, NULL, NULL, &ftReplaced);
2942 ok( ret, "GetFileTime error (backup %d\n", GetLastError());
2943 ok(check_file_time(&ftReplaced, &ftReplacement, 20000000),
2944 "replaced file has wrong filetime %x%08x / %x%08x\n",
2945 ftReplaced.dwHighDateTime, ftReplaced.dwLowDateTime,
2946 ftReplacement.dwHighDateTime, ftReplacement.dwLowDateTime );
2947 CloseHandle(hReplacedFile);
2948 }
2949 else
2950 skip("couldn't open replacement file, skipping tests\n");
2951
2952 /* re-create replacement file for pass w/o backup (blank) */
2953 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2954 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2955 /* perform replacement w/o backup
2956 * TODO: flags are not implemented
2957 */
2958 SetLastError(0xdeadbeef);
2959 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
2960 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2961 "ReplaceFileA: unexpected error %d\n", GetLastError());
2962
2963 /* re-create replacement file for pass w/ backup (backup-file not existing) */
2964 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2965 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2966 ret = DeleteFileA(backup);
2967 ok(ret, "DeleteFileA: error (backup) %d\n", GetLastError());
2968 /* perform replacement w/ backup (no pre-existing backup)
2969 * TODO: flags are not implemented
2970 */
2971 SetLastError(0xdeadbeef);
2972 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2973 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2974 "ReplaceFileA: unexpected error %d\n", GetLastError());
2975 if (ret)
2976 removeBackup = TRUE;
2977
2978 /* re-create replacement file for pass w/ no permissions to "replaced" */
2979 ret = GetTempFileNameA(temp_path, prefix, 0, replacement);
2980 ok(ret != 0, "GetTempFileNameA error (replacement) %d\n", GetLastError());
2981 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_READONLY);
2982 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2983 "SetFileAttributesA: error setting to read only %d\n", GetLastError());
2984 /* perform replacement w/ backup (no permission to "replaced")
2985 * TODO: flags are not implemented
2986 */
2987 SetLastError(0xdeadbeef);
2988 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
2989 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED, "ReplaceFileA: unexpected error %d\n", GetLastError());
2990 /* make sure that the replacement file still exists */
2991 hReplacementFile = CreateFileA(replacement, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
2992 ok(hReplacementFile != INVALID_HANDLE_VALUE ||
2993 broken(GetLastError() == ERROR_FILE_NOT_FOUND), /* win2k */
2994 "unexpected error, replacement file should still exist %d\n", GetLastError());
2995 CloseHandle(hReplacementFile);
2996 ret = SetFileAttributesA(replaced, FILE_ATTRIBUTE_NORMAL);
2997 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
2998 "SetFileAttributesA: error setting to normal %d\n", GetLastError());
2999
3000 /* replacement file still exists, make pass w/o "replaced" */
3001 ret = DeleteFileA(replaced);
3002 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3003 "DeleteFileA: error (replaced) %d\n", GetLastError());
3004 /* perform replacement w/ backup (no pre-existing backup or "replaced")
3005 * TODO: flags are not implemented
3006 */
3007 SetLastError(0xdeadbeef);
3008 ret = pReplaceFileA(replaced, replacement, backup, 0, 0, 0);
3009 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3010 GetLastError() == ERROR_ACCESS_DENIED),
3011 "ReplaceFileA: unexpected error %d\n", GetLastError());
3012
3013 /* perform replacement w/o existing "replacement" file
3014 * TODO: flags are not implemented
3015 */
3016 SetLastError(0xdeadbeef);
3017 ret = pReplaceFileA(replaced, replacement, NULL, 0, 0, 0);
3018 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3019 GetLastError() == ERROR_ACCESS_DENIED),
3020 "ReplaceFileA: unexpected error %d\n", GetLastError());
3021 DeleteFileA( replacement );
3022
3023 /*
3024 * if the first round (w/ backup) worked then as long as there is no
3025 * failure then there is no need to check this round (w/ backup is the
3026 * more complete case)
3027 */
3028
3029 /* delete temporary files, replacement and replaced are already deleted */
3030 if (removeBackup)
3031 {
3032 ret = DeleteFileA(backup);
3033 ok(ret ||
3034 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3035 "DeleteFileA: error (backup) %d\n", GetLastError());
3036 }
3037 }
3038
3039 /*
3040 * ReplaceFileW is a simpler case of ReplaceFileA, there is no
3041 * need to be as thorough.
3042 */
3043 static void test_ReplaceFileW(void)
3044 {
3045 WCHAR replaced[MAX_PATH], replacement[MAX_PATH], backup[MAX_PATH];
3046 static const WCHAR prefix[] = {'p','f','x',0};
3047 WCHAR temp_path[MAX_PATH];
3048 DWORD ret;
3049 BOOL removeBackup = FALSE;
3050
3051 if (!pReplaceFileW)
3052 {
3053 win_skip("ReplaceFileW() is missing\n");
3054 return;
3055 }
3056
3057 ret = GetTempPathW(MAX_PATH, temp_path);
3058 if (ret == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
3059 {
3060 win_skip("GetTempPathW is not available\n");
3061 return;
3062 }
3063 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
3064 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
3065
3066 ret = GetTempFileNameW(temp_path, prefix, 0, replaced);
3067 ok(ret != 0, "GetTempFileNameW error (replaced) %d\n", GetLastError());
3068
3069 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3070 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3071
3072 ret = GetTempFileNameW(temp_path, prefix, 0, backup);
3073 ok(ret != 0, "GetTempFileNameW error (backup) %d\n", GetLastError());
3074
3075 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3076 ok(ret, "ReplaceFileW: error %d\n", GetLastError());
3077
3078 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3079 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3080 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3081 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3082 "ReplaceFileW: error %d\n", GetLastError());
3083
3084 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3085 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3086 ret = DeleteFileW(backup);
3087 ok(ret, "DeleteFileW: error (backup) %d\n", GetLastError());
3088 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3089 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3090 "ReplaceFileW: error %d\n", GetLastError());
3091
3092 ret = GetTempFileNameW(temp_path, prefix, 0, replacement);
3093 ok(ret != 0, "GetTempFileNameW error (replacement) %d\n", GetLastError());
3094 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_READONLY);
3095 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3096 "SetFileAttributesW: error setting to read only %d\n", GetLastError());
3097
3098 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3099 ok(ret != ERROR_UNABLE_TO_REMOVE_REPLACED,
3100 "ReplaceFileW: unexpected error %d\n", GetLastError());
3101 ret = SetFileAttributesW(replaced, FILE_ATTRIBUTE_NORMAL);
3102 ok(ret || GetLastError() == ERROR_ACCESS_DENIED,
3103 "SetFileAttributesW: error setting to normal %d\n", GetLastError());
3104 if (ret)
3105 removeBackup = TRUE;
3106
3107 ret = DeleteFileW(replaced);
3108 ok(ret, "DeleteFileW: error (replaced) %d\n", GetLastError());
3109 ret = pReplaceFileW(replaced, replacement, backup, 0, 0, 0);
3110 ok(!ret, "ReplaceFileW: error %d\n", GetLastError());
3111
3112 ret = pReplaceFileW(replaced, replacement, NULL, 0, 0, 0);
3113 ok(!ret && (GetLastError() == ERROR_FILE_NOT_FOUND ||
3114 GetLastError() == ERROR_ACCESS_DENIED),
3115 "ReplaceFileW: unexpected error %d\n", GetLastError());
3116 DeleteFileW( replacement );
3117
3118 if (removeBackup)
3119 {
3120 ret = DeleteFileW(backup);
3121 ok(ret ||
3122 broken(GetLastError() == ERROR_ACCESS_DENIED), /* win2k */
3123 "DeleteFileW: error (backup) %d\n", GetLastError());
3124 }
3125 }
3126
3127 START_TEST(file)
3128 {
3129 InitFunctionPointers();
3130
3131 test__hread( );
3132 test__hwrite( );
3133 test__lclose( );
3134 test__lcreat( );
3135 test__llseek( );
3136 test__llopen( );
3137 test__lread( );
3138 test__lwrite( );
3139 test_GetTempFileNameA();
3140 test_CopyFileA();
3141 test_CopyFileW();
3142 test_CreateFileA();
3143 test_CreateFileW();
3144 test_DeleteFileA();
3145 test_DeleteFileW();
3146 test_MoveFileA();
3147 test_MoveFileW();
3148 test_FindFirstFileA();
3149 test_FindNextFileA();
3150 test_FindFirstFileExA(0);
3151 /* FindExLimitToDirectories is ignored if the file system doesn't support directory filtering */
3152 test_FindFirstFileExA(FindExSearchLimitToDirectories);
3153 test_LockFile();
3154 test_file_sharing();
3155 test_offset_in_overlapped_structure();
3156 test_MapFile();
3157 test_GetFileType();
3158 test_async_file_errors();
3159 test_read_write();
3160 test_OpenFile();
3161 test_overlapped();
3162 test_RemoveDirectory();
3163 test_ReplaceFileA();
3164 test_ReplaceFileW();
3165 }