1 /*
2 * Unit tests for file functions in Wine
3 *
4 * Copyright (c) 2002, 2004 Jakob Eriksson
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 */
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 /* ReplaceFile requires Windows 2000 or newer */
27 #define _WIN32_WINNT 0x0500
28
29 #include "wine/test.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33
34 static HANDLE (WINAPI *pFindFirstFileExA)(LPCSTR,FINDEX_INFO_LEVELS,LPVOID,FINDEX_SEARCH_OPS,LPVOID,DWORD);
35 static BOOL (WINAPI *pReplaceFileA)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
36 static BOOL (WINAPI *pReplaceFileW)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
37
38 /* keep filename and filenameW the same */
39 static const char filename[] = "testfile.xxx";
40 static const WCHAR filenameW[] = { 't','e','s','t','f','i','l','e','.','x','x','x',0 };
41 static const char sillytext[] =
42 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
43 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
44 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
45 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
46 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
52
53 static void InitFunctionPointers(void)
54 {
55 HMODULE hkernel32 = GetModuleHandleA("kernel32");
56
57 pFindFirstFileExA=(void*)GetProcAddress(hkernel32, "FindFirstFileExA");
58 pReplaceFileA=(void*)GetProcAddress(hkernel32, "ReplaceFileA");
59 pReplaceFileW=(void*)GetProcAddress(hkernel32, "ReplaceFileW");
60 }
61
62 static void test__hread( void )
63 {
64 HFILE filehandle;
65 char buffer[10000];
66 long bytes_read;
67 long bytes_wanted;
68 long i;
69 BOOL ret;
70
71 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
72 DeleteFileA( filename );
73 filehandle = _lcreat( filename, 0 );
74 if (filehandle == HFILE_ERROR)
75 {
76 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
77 return;
78 }
79
80 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
81
82 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
83
84 filehandle = _lopen( filename, OF_READ );
85
86 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError( ) );
87
88 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
89
90 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
91
92 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
93 {
94 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
95 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
96 for (i = 0; i < bytes_wanted; i++)
97 {
98 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
99 }
100 }
101
102 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
103
104 ret = DeleteFileA( filename );
105 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
106 }
107
108
109 static void test__hwrite( void )
110 {
111 HFILE filehandle;
112 char buffer[10000];
113 long bytes_read;
114 long bytes_written;
115 long blocks;
116 long i;
117 char *contents;
118 HLOCAL memory_object;
119 char checksum[1];
120 BOOL ret;
121
122 filehandle = _lcreat( filename, 0 );
123 if (filehandle == HFILE_ERROR)
124 {
125 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
126 return;
127 }
128
129 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains\n" );
130
131 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
132
133 filehandle = _lopen( filename, OF_READ );
134
135 bytes_read = _hread( filehandle, buffer, 1);
136
137 ok( 0 == bytes_read, "file read size error\n" );
138
139 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
140
141 filehandle = _lopen( filename, OF_READWRITE );
142
143 bytes_written = 0;
144 checksum[0] = '\0';
145 srand( (unsigned)time( NULL ) );
146 for (blocks = 0; blocks < 100; blocks++)
147 {
148 for (i = 0; i < (long)sizeof( buffer ); i++)
149 {
150 buffer[i] = rand( );
151 checksum[0] = checksum[0] + buffer[i];
152 }
153 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
154 bytes_written = bytes_written + sizeof( buffer );
155 }
156
157 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
158 bytes_written++;
159
160 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
161
162 memory_object = LocalAlloc( LPTR, bytes_written );
163
164 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)\n" );
165
166 contents = LocalLock( memory_object );
167
168 filehandle = _lopen( filename, OF_READ );
169
170 contents = LocalLock( memory_object );
171
172 ok( NULL != contents, "LocalLock whines\n" );
173
174 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
175
176 checksum[0] = '\0';
177 i = 0;
178 do
179 {
180 checksum[0] = checksum[0] + contents[i];
181 i++;
182 }
183 while (i < bytes_written - 1);
184
185 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
186
187 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
188
189 ret = DeleteFileA( filename );
190 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
191 }
192
193
194 static void test__lclose( void )
195 {
196 HFILE filehandle;
197 BOOL ret;
198
199 filehandle = _lcreat( filename, 0 );
200 if (filehandle == HFILE_ERROR)
201 {
202 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
203 return;
204 }
205
206 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
207
208 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
209
210 ret = DeleteFileA( filename );
211 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError( ) );
212 }
213
214
215 static void test__lcreat( void )
216 {
217 HFILE filehandle;
218 char buffer[10000];
219 WIN32_FIND_DATAA search_results;
220 char slashname[] = "testfi/";
221 int err;
222 HANDLE find;
223 BOOL ret;
224
225 filehandle = _lcreat( filename, 0 );
226 if (filehandle == HFILE_ERROR)
227 {
228 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
229 return;
230 }
231
232 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
233
234 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
235
236 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
237
238 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
239
240 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
241
242 ret = DeleteFileA(filename);
243 ok( ret != 0, "DeleteFile failed (%d)\n", GetLastError());
244
245 filehandle = _lcreat( filename, 1 ); /* readonly */
246 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
247
248 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less\n" );
249
250 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
251
252 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file\n" );
253
254 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file\n" );
255
256 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file\n" );
257
258 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!\n" );
259
260 filehandle = _lcreat( filename, 2 );
261 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
262
263 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
264
265 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
266
267 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
268
269 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
270
271 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
272
273 ret = DeleteFileA( filename );
274 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
275
276 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
277 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)\n", filename, GetLastError( ) );
278
279 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
280
281 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
282
283 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value\n" );
284
285 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
286
287 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file\n" );
288
289 ret = DeleteFileA( filename );
290 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
291
292 filehandle=_lcreat (slashname, 0); /* illegal name */
293 if (HFILE_ERROR==filehandle) {
294 err=GetLastError ();
295 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
296 "creating file \"%s\" failed with error %d\n", slashname, err);
297 } else { /* only NT succeeds */
298 _lclose(filehandle);
299 find=FindFirstFileA (slashname, &search_results);
300 if (INVALID_HANDLE_VALUE!=find)
301 {
302 ret = FindClose (find);
303 ok (0 != ret, "FindClose complains (%d)\n", GetLastError ());
304 slashname[strlen(slashname)-1]=0;
305 ok (!strcmp (slashname, search_results.cFileName),
306 "found unexpected name \"%s\"\n", search_results.cFileName);
307 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
308 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
309 search_results.dwFileAttributes);
310 }
311 ret = DeleteFileA( slashname );
312 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
313 }
314
315 filehandle=_lcreat (filename, 8); /* illegal attribute */
316 if (HFILE_ERROR==filehandle)
317 ok (0, "couldn't create volume label \"%s\"\n", filename);
318 else {
319 _lclose(filehandle);
320 find=FindFirstFileA (filename, &search_results);
321 if (INVALID_HANDLE_VALUE==find)
322 ok (0, "file \"%s\" not found\n", filename);
323 else {
324 ret = FindClose(find);
325 ok ( 0 != ret, "FindClose complains (%d)\n", GetLastError ());
326 ok (!strcmp (filename, search_results.cFileName),
327 "found unexpected name \"%s\"\n", search_results.cFileName);
328 search_results.dwFileAttributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
329 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
330 "attributes of file \"%s\" are 0x%04x\n", search_results.cFileName,
331 search_results.dwFileAttributes);
332 }
333 ret = DeleteFileA( filename );
334 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
335 }
336 }
337
338
339 static void test__llseek( void )
340 {
341 INT i;
342 HFILE filehandle;
343 char buffer[1];
344 long bytes_read;
345 BOOL ret;
346
347 filehandle = _lcreat( filename, 0 );
348 if (filehandle == HFILE_ERROR)
349 {
350 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
351 return;
352 }
353
354 for (i = 0; i < 400; i++)
355 {
356 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
357 }
358 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek\n" );
359 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek\n" );
360
361 bytes_read = _hread( filehandle, buffer, 1);
362 ok( 1 == bytes_read, "file read size error\n" );
363 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking\n" );
364 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek\n" );
365
366 bytes_read = _hread( filehandle, buffer, 1);
367 ok( 1 == bytes_read, "file read size error\n" );
368 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking\n" );
369 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers\n" );
370 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
371
372 ret = DeleteFileA( filename );
373 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
374 }
375
376
377 static void test__llopen( void )
378 {
379 HFILE filehandle;
380 UINT bytes_read;
381 char buffer[10000];
382 BOOL ret;
383
384 filehandle = _lcreat( filename, 0 );
385 if (filehandle == HFILE_ERROR)
386 {
387 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
388 return;
389 }
390
391 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
392 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
393
394 filehandle = _lopen( filename, OF_READ );
395 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!\n" );
396 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
397 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
398 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
399
400 filehandle = _lopen( filename, OF_READWRITE );
401 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
402 ok( strlen( sillytext ) == bytes_read, "file read size error\n" );
403 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
404 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
405
406 filehandle = _lopen( filename, OF_WRITE );
407 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file\n" );
408 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine\n" );
409 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
410
411 ret = DeleteFileA( filename );
412 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
413 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
414 }
415
416
417 static void test__lread( void )
418 {
419 HFILE filehandle;
420 char buffer[10000];
421 long bytes_read;
422 UINT bytes_wanted;
423 UINT i;
424 BOOL ret;
425
426 filehandle = _lcreat( filename, 0 );
427 if (filehandle == HFILE_ERROR)
428 {
429 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
430 return;
431 }
432
433 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains\n" );
434
435 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
436
437 filehandle = _lopen( filename, OF_READ );
438
439 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)\n", filename, GetLastError());
440
441 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
442
443 ok( lstrlenA( sillytext ) == bytes_read, "file read size error\n" );
444
445 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
446 {
447 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains\n" );
448 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value\n" );
449 for (i = 0; i < bytes_wanted; i++)
450 {
451 ok( buffer[i] == sillytext[i], "that's not what's written\n" );
452 }
453 }
454
455 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
456
457 ret = DeleteFileA( filename );
458 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
459 }
460
461
462 static void test__lwrite( void )
463 {
464 HFILE filehandle;
465 char buffer[10000];
466 long bytes_read;
467 long bytes_written;
468 long blocks;
469 long i;
470 char *contents;
471 HLOCAL memory_object;
472 char checksum[1];
473 BOOL ret;
474
475 filehandle = _lcreat( filename, 0 );
476 if (filehandle == HFILE_ERROR)
477 {
478 ok(0,"couldn't create file \"%s\" (err=%d)\n",filename,GetLastError());
479 return;
480 }
481
482 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains\n" );
483
484 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
485
486 filehandle = _lopen( filename, OF_READ );
487
488 bytes_read = _hread( filehandle, buffer, 1);
489
490 ok( 0 == bytes_read, "file read size error\n" );
491
492 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains\n" );
493
494 filehandle = _lopen( filename, OF_READWRITE );
495
496 bytes_written = 0;
497 checksum[0] = '\0';
498 srand( (unsigned)time( NULL ) );
499 for (blocks = 0; blocks < 100; blocks++)
500 {
501 for (i = 0; i < (long)sizeof( buffer ); i++)
502 {
503 buffer[i] = rand( );
504 checksum[0] = checksum[0] + buffer[i];
505 }
506 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains\n" );
507 bytes_written = bytes_written + sizeof( buffer );
508 }
509
510 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains\n" );
511 bytes_written++;
512
513 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
514
515 memory_object = LocalAlloc( LPTR, bytes_written );
516
517 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory\n" );
518
519 contents = LocalLock( memory_object );
520
521 filehandle = _lopen( filename, OF_READ );
522
523 contents = LocalLock( memory_object );
524
525 ok( NULL != contents, "LocalLock whines\n" );
526
527 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length\n" );
528
529 checksum[0] = '\0';
530 i = 0;
531 do
532 {
533 checksum[0] += contents[i];
534 i++;
535 }
536 while (i < bytes_written - 1);
537
538 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum\n" );
539
540 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains\n" );
541
542 ret = DeleteFileA( filename );
543 ok( ret, "DeleteFile failed (%d)\n", GetLastError( ) );
544 }
545
546 static void test_CopyFileA(void)
547 {
548 char temp_path[MAX_PATH];
549 char source[MAX_PATH], dest[MAX_PATH];
550 static const char prefix[] = "pfx";
551 HANDLE hfile;
552 FILETIME ft1, ft2;
553 char buf[10];
554 DWORD ret;
555 BOOL retok;
556
557 ret = GetTempPathA(MAX_PATH, temp_path);
558 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
559 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
560
561 ret = GetTempFileNameA(temp_path, prefix, 0, source);
562 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
563
564 /* make the source have not zero size */
565 hfile = CreateFileA(source, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0 );
566 ok(hfile != INVALID_HANDLE_VALUE, "failed to open source file\n");
567 retok = WriteFile(hfile, prefix, sizeof(prefix), &ret, NULL );
568 ok( retok && ret == sizeof(prefix),
569 "WriteFile error %d\n", GetLastError());
570 ok(GetFileSize(hfile, NULL) == sizeof(prefix), "source file has wrong size\n");
571 /* get the file time and change it to prove the difference */
572 ret = GetFileTime(hfile, NULL, NULL, &ft1);
573 ok( ret, "GetFileTime error %d\n", GetLastError());
574 ft1.dwLowDateTime -= 600000000; /* 60 second */
575 ret = SetFileTime(hfile, NULL, NULL, &ft1);
576 ok( ret, "SetFileTime error %d\n", GetLastError());
577 GetFileTime(hfile, NULL, NULL, &ft1); /* get the actual time back */
578 CloseHandle(hfile);
579
580 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
581 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
582
583 SetLastError(0xdeadbeef);
584 ret = CopyFileA(source, dest, TRUE);
585 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
586 "CopyFileA: unexpected error %d\n", GetLastError());
587
588 ret = CopyFileA(source, dest, FALSE);
589 ok(ret, "CopyFileA: error %d\n", GetLastError());
590
591 /* make sure that destination has correct size */
592 hfile = CreateFileA(dest, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
593 ok(hfile != INVALID_HANDLE_VALUE, "failed to open destination file\n");
594 ret = GetFileSize(hfile, NULL);
595 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
596
597 /* make sure that destination has the same filetime */
598 ret = GetFileTime(hfile, NULL, NULL, &ft2);
599 ok( ret, "GetFileTime error %d\n", GetLastError());
600 ok(CompareFileTime(&ft1, &ft2) == 0, "destination file has wrong filetime\n");
601
602 SetLastError(0xdeadbeef);
603 ret = CopyFileA(source, dest, FALSE);
604 ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION,
605 "CopyFileA: ret = %d, unexpected error %d\n", ret, GetLastError());
606
607 /* make sure that destination still has correct size */
608 ret = GetFileSize(hfile, NULL);
609 ok(ret == sizeof(prefix), "destination file has wrong size %d\n", ret);
610 retok = ReadFile(hfile, buf, sizeof(buf), &ret, NULL);
611 ok( retok && ret == sizeof(prefix),
612 "ReadFile: error %d\n", GetLastError());
613 ok(!memcmp(prefix, buf, sizeof(prefix)), "buffer contents mismatch\n");
614 CloseHandle(hfile);
615
616 ret = DeleteFileA(source);
617 ok(ret, "DeleteFileA: error %d\n", GetLastError());
618 ret = DeleteFileA(dest);
619 ok(ret, "DeleteFileA: error %d\n", GetLastError());
620 }
621
622 static void test_CopyFileW(void)
623 {
624 WCHAR temp_path[MAX_PATH];
625 WCHAR source[MAX_PATH], dest[MAX_PATH];
626 static const WCHAR prefix[] = {'p','f','x',0};
627 DWORD ret;
628
629 ret = GetTempPathW(MAX_PATH, temp_path);
630 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
631 return;
632 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
633 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
634
635 ret = GetTempFileNameW(temp_path, prefix, 0, source);
636 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
637
638 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
639 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
640
641 ret = CopyFileW(source, dest, TRUE);
642 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
643 "CopyFileW: unexpected error %d\n", GetLastError());
644
645 ret = CopyFileW(source, dest, FALSE);
646 ok(ret, "CopyFileW: error %d\n", GetLastError());
647
648 ret = DeleteFileW(source);
649 ok(ret, "DeleteFileW: error %d\n", GetLastError());
650 ret = DeleteFileW(dest);
651 ok(ret, "DeleteFileW: error %d\n", GetLastError());
652 }
653
654 static void test_CreateFileA(void)
655 {
656 HANDLE hFile;
657 char temp_path[MAX_PATH];
658 char filename[MAX_PATH];
659 static const char prefix[] = "pfx";
660 DWORD ret;
661
662 ret = GetTempPathA(MAX_PATH, temp_path);
663 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
664 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
665
666 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
667 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
668
669 SetLastError(0xdeadbeef);
670 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
671 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
672 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
673 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
674
675 SetLastError(0xdeadbeef);
676 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
677 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
678 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
679 "hFile %p, last error %u\n", hFile, GetLastError());
680
681 CloseHandle(hFile);
682
683 SetLastError(0xdeadbeef);
684 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
685 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
686 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
687 "hFile %p, last error %u\n", hFile, GetLastError());
688
689 CloseHandle(hFile);
690
691 ret = DeleteFileA(filename);
692 ok(ret, "DeleteFileA: error %d\n", GetLastError());
693
694 SetLastError(0xdeadbeef);
695 hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
696 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
697 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
698 "hFile %p, last error %u\n", hFile, GetLastError());
699
700 CloseHandle(hFile);
701
702 ret = DeleteFileA(filename);
703 ok(ret, "DeleteFileA: error %d\n", GetLastError());
704 }
705
706 static void test_CreateFileW(void)
707 {
708 HANDLE hFile;
709 WCHAR temp_path[MAX_PATH];
710 WCHAR filename[MAX_PATH];
711 static const WCHAR emptyW[]={'\0'};
712 static const WCHAR prefix[] = {'p','f','x',0};
713 static const WCHAR bogus[] = { '\\', '\\', '.', '\\', 'B', 'O', 'G', 'U', 'S', 0 };
714 DWORD ret;
715
716 ret = GetTempPathW(MAX_PATH, temp_path);
717 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
718 return;
719 ok(ret != 0, "GetTempPathW error %d\n", GetLastError());
720 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
721
722 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
723 ok(ret != 0, "GetTempFileNameW error %d\n", GetLastError());
724
725 SetLastError(0xdeadbeef);
726 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
727 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
728 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
729 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS\n");
730
731 SetLastError(0xdeadbeef);
732 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
733 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
734 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
735 "hFile %p, last error %u\n", hFile, GetLastError());
736
737 CloseHandle(hFile);
738
739 SetLastError(0xdeadbeef);
740 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
741 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
742 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == ERROR_ALREADY_EXISTS,
743 "hFile %p, last error %u\n", hFile, GetLastError());
744
745 CloseHandle(hFile);
746
747 ret = DeleteFileW(filename);
748 ok(ret, "DeleteFileW: error %d\n", GetLastError());
749
750 SetLastError(0xdeadbeef);
751 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
752 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
753 ok(hFile != INVALID_HANDLE_VALUE && GetLastError() == 0,
754 "hFile %p, last error %u\n", hFile, GetLastError());
755
756 CloseHandle(hFile);
757
758 ret = DeleteFileW(filename);
759 ok(ret, "DeleteFileW: error %d\n", GetLastError());
760
761 if (0)
762 {
763 /* this crashes on NT4.0 */
764 hFile = CreateFileW(NULL, GENERIC_READ, 0, NULL,
765 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
766 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
767 "CreateFileW(NULL) returned ret=%p error=%u\n",hFile,GetLastError());
768 }
769
770 hFile = CreateFileW(emptyW, GENERIC_READ, 0, NULL,
771 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
772 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
773 "CreateFileW(\"\") returned ret=%p error=%d\n",hFile,GetLastError());
774
775 /* test the result of opening a nonexistent driver name */
776 hFile = CreateFileW(bogus, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
777 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
778 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
779 "CreateFileW on invalid VxD name returned ret=%p error=%d\n",hFil