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