blob: ee4b10b60c6796a32be4196d9f420b58658f3514 [file] [log] [blame]
Jakob Eriksson33151992002-04-29 17:14:50 +00001/*
Jakob Erikssone8449e62002-05-09 20:29:15 +00002 * Unit tests for file functions in Wine
Jakob Eriksson33151992002-04-29 17:14:50 +00003 *
Jakob Erikssone8449e62002-05-09 20:29:15 +00004 * Copyright (c) 2002 Jakob Eriksson
Jakob Eriksson33151992002-04-29 17:14:50 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Jakob Erikssone8449e62002-05-09 20:29:15 +000019 *
Jakob Eriksson33151992002-04-29 17:14:50 +000020 */
21
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000022#include <stdarg.h>
Jakob Eriksson33151992002-04-29 17:14:50 +000023#include <stdlib.h>
24#include <time.h>
25
Francois Gouget4c314002002-05-23 02:40:07 +000026#include "wine/test.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000027#include "windef.h"
Francois Gouget4c314002002-05-23 02:40:07 +000028#include "winbase.h"
29#include "winerror.h"
30
Jakob Eriksson33151992002-04-29 17:14:50 +000031LPCSTR filename = "testfile.xxx";
32LPCSTR sillytext =
33"en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
34"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
35"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
36"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
37"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
38"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
39"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
40"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
41"1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
42"sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
43
Jakob Erikssone8449e62002-05-09 20:29:15 +000044
Jakob Eriksson33151992002-04-29 17:14:50 +000045static void test__hread( void )
46{
47 HFILE filehandle;
48 char buffer[10000];
49 long bytes_read;
Jakob Erikssone8449e62002-05-09 20:29:15 +000050 long bytes_wanted;
Francois Gouget45d26132002-12-18 02:22:51 +000051 long i;
Jakob Eriksson33151992002-04-29 17:14:50 +000052
Alexandre Julliard034e39b2002-06-05 00:47:38 +000053 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
54 DeleteFileA( filename );
Jakob Eriksson33151992002-04-29 17:14:50 +000055 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +000056 if (filehandle == HFILE_ERROR)
57 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000058 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +000059 return;
60 }
Jakob Eriksson33151992002-04-29 17:14:50 +000061
Jakob Eriksson43b31772002-05-29 16:58:14 +000062 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000063
Jakob Eriksson43b31772002-05-29 16:58:14 +000064 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000065
66 filehandle = _lopen( filename, OF_READ );
Jakob Erikssonb02f0942002-05-06 20:12:55 +000067
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000068 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError( ) );
Francois Gougetc9493492002-05-23 16:29:37 +000069
Jakob Eriksson33151992002-04-29 17:14:50 +000070 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
Francois Gougetc9493492002-05-23 16:29:37 +000071
Francois Gouget45d26132002-12-18 02:22:51 +000072 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
Jakob Eriksson33151992002-04-29 17:14:50 +000073
Francois Gouget45d26132002-12-18 02:22:51 +000074 for (bytes_wanted = 0; bytes_wanted < lstrlenA( sillytext ); bytes_wanted++)
Jakob Eriksson33151992002-04-29 17:14:50 +000075 {
Jakob Eriksson43b31772002-05-29 16:58:14 +000076 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
77 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
Jakob Eriksson33151992002-04-29 17:14:50 +000078 for (i = 0; i < bytes_wanted; i++)
79 {
Jakob Eriksson43b31772002-05-29 16:58:14 +000080 ok( buffer[i] == sillytext[i], "that's not what's written" );
Jakob Eriksson33151992002-04-29 17:14:50 +000081 }
82 }
83
Jakob Eriksson43b31772002-05-29 16:58:14 +000084 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000085
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000086 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +000087}
88
Jakob Erikssone8449e62002-05-09 20:29:15 +000089
Jakob Eriksson33151992002-04-29 17:14:50 +000090static void test__hwrite( void )
91{
92 HFILE filehandle;
93 char buffer[10000];
94 long bytes_read;
Francois Gouget45d26132002-12-18 02:22:51 +000095 long bytes_written;
96 long blocks;
97 long i;
Jakob Eriksson33151992002-04-29 17:14:50 +000098 char *contents;
99 HLOCAL memory_object;
100 char checksum[1];
101
102 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000103 if (filehandle == HFILE_ERROR)
104 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000105 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000106 return;
107 }
Jakob Eriksson33151992002-04-29 17:14:50 +0000108
Jakob Eriksson43b31772002-05-29 16:58:14 +0000109 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000110
Jakob Eriksson43b31772002-05-29 16:58:14 +0000111 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000112
113 filehandle = _lopen( filename, OF_READ );
Francois Gougetc9493492002-05-23 16:29:37 +0000114
Jakob Eriksson33151992002-04-29 17:14:50 +0000115 bytes_read = _hread( filehandle, buffer, 1);
116
Jakob Eriksson43b31772002-05-29 16:58:14 +0000117 ok( 0 == bytes_read, "file read size error" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000118
Jakob Eriksson43b31772002-05-29 16:58:14 +0000119 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000120
121 filehandle = _lopen( filename, OF_READWRITE );
122
123 bytes_written = 0;
124 checksum[0] = '\0';
125 srand( (unsigned)time( NULL ) );
126 for (blocks = 0; blocks < 100; blocks++)
127 {
Jon Griffiths7d75c142003-07-19 03:06:19 +0000128 for (i = 0; i < (long)sizeof( buffer ); i++)
Jakob Eriksson33151992002-04-29 17:14:50 +0000129 {
130 buffer[i] = rand( );
131 checksum[0] = checksum[0] + buffer[i];
132 }
Jakob Eriksson43b31772002-05-29 16:58:14 +0000133 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000134 bytes_written = bytes_written + sizeof( buffer );
135 }
136
Jakob Eriksson43b31772002-05-29 16:58:14 +0000137 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000138 bytes_written++;
139
Jakob Eriksson43b31772002-05-29 16:58:14 +0000140 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000141
142 memory_object = LocalAlloc( LPTR, bytes_written );
143
144 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
145
146 contents = LocalLock( memory_object );
147
148 filehandle = _lopen( filename, OF_READ );
149
150 contents = LocalLock( memory_object );
151
Jakob Eriksson43b31772002-05-29 16:58:14 +0000152 ok( NULL != contents, "LocalLock whines" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000153
Jakob Eriksson43b31772002-05-29 16:58:14 +0000154 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000155
156 checksum[0] = '\0';
157 i = 0;
158 do
159 {
160 checksum[0] = checksum[0] + contents[i];
161 i++;
162 }
163 while (i < bytes_written - 1);
164
Jakob Eriksson43b31772002-05-29 16:58:14 +0000165 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000166
Jakob Eriksson43b31772002-05-29 16:58:14 +0000167 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000168
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000169 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +0000170}
171
Jakob Erikssone8449e62002-05-09 20:29:15 +0000172
Jakob Eriksson33151992002-04-29 17:14:50 +0000173static void test__lclose( void )
174{
175 HFILE filehandle;
176
177 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000178 if (filehandle == HFILE_ERROR)
179 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000180 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000181 return;
182 }
Jakob Eriksson33151992002-04-29 17:14:50 +0000183
Jakob Eriksson43b31772002-05-29 16:58:14 +0000184 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000185
Jakob Eriksson43b31772002-05-29 16:58:14 +0000186 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000187
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000188 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +0000189}
190
191
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000192static void test__lcreat( void )
193{
194 HFILE filehandle;
195 char buffer[10000];
196 WIN32_FIND_DATAA search_results;
Ferenc Wagner18f7bfb2003-05-19 21:50:32 +0000197 char slashname[] = "testfi/";
Ferenc Wagner4b28f0f2003-10-21 23:42:50 +0000198 int err;
Ferenc Wagner18f7bfb2003-05-19 21:50:32 +0000199 HANDLE find;
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000200
201 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000202 if (filehandle == HFILE_ERROR)
203 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000204 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000205 return;
206 }
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000207
Jakob Eriksson43b31772002-05-29 16:58:14 +0000208 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000209
Jakob Eriksson43b31772002-05-29 16:58:14 +0000210 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000211
Francois Gouget45d26132002-12-18 02:22:51 +0000212 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000213
Jakob Eriksson43b31772002-05-29 16:58:14 +0000214 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000215
216 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
217
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000218 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%ld)", GetLastError());
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000219
Jakob Eriksson43b31772002-05-29 16:58:14 +0000220 filehandle = _lcreat( filename, 1 ); /* readonly */
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000221 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000222
Jakob Eriksson43b31772002-05-29 16:58:14 +0000223 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000224
Jakob Eriksson43b31772002-05-29 16:58:14 +0000225 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000226
227 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
228
Alexandre Julliard034e39b2002-06-05 00:47:38 +0000229 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000230
Alexandre Julliard034e39b2002-06-05 00:47:38 +0000231 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000232
Alexandre Julliard034e39b2002-06-05 00:47:38 +0000233 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000234
235 filehandle = _lcreat( filename, 2 );
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000236 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000237
Jakob Eriksson43b31772002-05-29 16:58:14 +0000238 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000239
Jakob Eriksson43b31772002-05-29 16:58:14 +0000240 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000241
Francois Gouget45d26132002-12-18 02:22:51 +0000242 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000243
Jakob Eriksson43b31772002-05-29 16:58:14 +0000244 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000245
Jakob Eriksson43b31772002-05-29 16:58:14 +0000246 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000247
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000248 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Francois Gougetc9493492002-05-23 16:29:37 +0000249
Jakob Eriksson43b31772002-05-29 16:58:14 +0000250 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000251 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%ld)", filename, GetLastError( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000252
Jakob Eriksson43b31772002-05-29 16:58:14 +0000253 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000254
Jakob Eriksson43b31772002-05-29 16:58:14 +0000255 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000256
Francois Gouget45d26132002-12-18 02:22:51 +0000257 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == lstrlenA( sillytext ), "erratic _hread return value" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000258
Jakob Eriksson43b31772002-05-29 16:58:14 +0000259 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000260
Jakob Eriksson43b31772002-05-29 16:58:14 +0000261 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000262
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000263 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Ferenc Wagner18f7bfb2003-05-19 21:50:32 +0000264
265 filehandle=_lcreat (slashname, 0); /* illegal name */
Ferenc Wagner4b28f0f2003-10-21 23:42:50 +0000266 if (HFILE_ERROR==filehandle) {
267 err=GetLastError ();
268 ok (err==ERROR_INVALID_NAME || err==ERROR_PATH_NOT_FOUND,
269 "creating file \"%s\" failed with error %d", slashname, err);
270 } else { /* only NT succeeds */
Ferenc Wagner18f7bfb2003-05-19 21:50:32 +0000271 _lclose(filehandle);
272 find=FindFirstFileA (slashname, &search_results);
273 if (INVALID_HANDLE_VALUE==find)
274 ok (0, "file \"%s\" not found", slashname);
275 else {
276 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
277 slashname[strlen(slashname)-1]=0;
278 ok (!strcmp (slashname, search_results.cFileName),
279 "found unexpected name \"%s\"", search_results.cFileName);
280 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
281 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
282 search_results.dwFileAttributes);
283 }
284 ok (0!=DeleteFileA (slashname), "Can't delete \"%s\" (%ld)", slashname,
285 GetLastError ());
286 }
287
288 filehandle=_lcreat (filename, 8); /* illegal attribute */
289 if (HFILE_ERROR==filehandle)
290 ok (0, "couldn't create volume label \"%s\"", filename);
291 else {
292 _lclose(filehandle);
293 find=FindFirstFileA (filename, &search_results);
294 if (INVALID_HANDLE_VALUE==find)
295 ok (0, "file \"%s\" not found", filename);
296 else {
297 ok (0!=FindClose (find), "FindClose complains (%ld)", GetLastError ());
298 ok (!strcmp (filename, search_results.cFileName),
299 "found unexpected name \"%s\"", search_results.cFileName);
300 ok (FILE_ATTRIBUTE_ARCHIVE==search_results.dwFileAttributes,
301 "attributes of file \"%s\" are 0x%04lx", search_results.cFileName,
302 search_results.dwFileAttributes);
303 }
304 ok (0!=DeleteFileA (filename), "Can't delete \"%s\" (%ld)", slashname,
305 GetLastError ());
306 }
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000307}
308
309
Hans Leidekker74992422003-12-08 22:21:20 +0000310static void test__llseek( void )
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000311{
312 INT i;
313 HFILE filehandle;
314 char buffer[1];
315 long bytes_read;
316
317 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000318 if (filehandle == HFILE_ERROR)
319 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000320 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000321 return;
322 }
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000323
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000324 for (i = 0; i < 400; i++)
325 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000326 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000327 }
328 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
329 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
330
331 bytes_read = _hread( filehandle, buffer, 1);
Jakob Eriksson43b31772002-05-29 16:58:14 +0000332 ok( 1 == bytes_read, "file read size error" );
333 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000334 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
335
336 bytes_read = _hread( filehandle, buffer, 1);
Jakob Eriksson43b31772002-05-29 16:58:14 +0000337 ok( 1 == bytes_read, "file read size error" );
338 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
339 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
340 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000341
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000342 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000343}
344
Jakob Erikssone8449e62002-05-09 20:29:15 +0000345
346static void test__llopen( void )
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000347{
348 HFILE filehandle;
349 UINT bytes_read;
350 char buffer[10000];
351
352 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000353 if (filehandle == HFILE_ERROR)
354 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000355 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000356 return;
357 }
358
Jakob Eriksson43b31772002-05-29 16:58:14 +0000359 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
360 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000361
362 filehandle = _lopen( filename, OF_READ );
363 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
364 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000365 ok( strlen( sillytext ) == bytes_read, "file read size error" );
366 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Francois Gougetc9493492002-05-23 16:29:37 +0000367
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000368 filehandle = _lopen( filename, OF_READWRITE );
369 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000370 ok( strlen( sillytext ) == bytes_read, "file read size error" );
371 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
372 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000373
374 filehandle = _lopen( filename, OF_WRITE );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000375 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
376 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
377 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000378
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000379 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000380 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000381}
382
Jakob Erikssone8449e62002-05-09 20:29:15 +0000383
384static void test__lread( void )
385{
386 HFILE filehandle;
387 char buffer[10000];
388 long bytes_read;
389 UINT bytes_wanted;
390 UINT i;
391
392 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000393 if (filehandle == HFILE_ERROR)
394 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000395 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000396 return;
397 }
Jakob Erikssone8449e62002-05-09 20:29:15 +0000398
Jakob Eriksson43b31772002-05-29 16:58:14 +0000399 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000400
Jakob Eriksson43b31772002-05-29 16:58:14 +0000401 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000402
403 filehandle = _lopen( filename, OF_READ );
404
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000405 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000406
Jakob Erikssone8449e62002-05-09 20:29:15 +0000407 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
Francois Gougetc9493492002-05-23 16:29:37 +0000408
Francois Gouget45d26132002-12-18 02:22:51 +0000409 ok( lstrlenA( sillytext ) == bytes_read, "file read size error" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000410
411 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
412 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000413 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
414 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000415 for (i = 0; i < bytes_wanted; i++)
416 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000417 ok( buffer[i] == sillytext[i], "that's not what's written" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000418 }
419 }
420
Jakob Eriksson43b31772002-05-29 16:58:14 +0000421 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000422
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000423 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000424}
425
426
427static void test__lwrite( void )
428{
429 HFILE filehandle;
430 char buffer[10000];
431 long bytes_read;
Francois Gouget45d26132002-12-18 02:22:51 +0000432 long bytes_written;
433 long blocks;
434 long i;
Jakob Erikssone8449e62002-05-09 20:29:15 +0000435 char *contents;
436 HLOCAL memory_object;
437 char checksum[1];
438
439 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000440 if (filehandle == HFILE_ERROR)
441 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000442 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000443 return;
444 }
Jakob Erikssone8449e62002-05-09 20:29:15 +0000445
Jakob Eriksson43b31772002-05-29 16:58:14 +0000446 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000447
Jakob Eriksson43b31772002-05-29 16:58:14 +0000448 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000449
450 filehandle = _lopen( filename, OF_READ );
Francois Gougetc9493492002-05-23 16:29:37 +0000451
Jakob Erikssone8449e62002-05-09 20:29:15 +0000452 bytes_read = _hread( filehandle, buffer, 1);
453
Jakob Eriksson43b31772002-05-29 16:58:14 +0000454 ok( 0 == bytes_read, "file read size error" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000455
Jakob Eriksson43b31772002-05-29 16:58:14 +0000456 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000457
458 filehandle = _lopen( filename, OF_READWRITE );
459
460 bytes_written = 0;
461 checksum[0] = '\0';
462 srand( (unsigned)time( NULL ) );
463 for (blocks = 0; blocks < 100; blocks++)
464 {
Jon Griffiths7d75c142003-07-19 03:06:19 +0000465 for (i = 0; i < (long)sizeof( buffer ); i++)
Jakob Erikssone8449e62002-05-09 20:29:15 +0000466 {
467 buffer[i] = rand( );
468 checksum[0] = checksum[0] + buffer[i];
469 }
Jakob Eriksson43b31772002-05-29 16:58:14 +0000470 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000471 bytes_written = bytes_written + sizeof( buffer );
472 }
473
Jakob Eriksson43b31772002-05-29 16:58:14 +0000474 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000475 bytes_written++;
476
Jakob Eriksson43b31772002-05-29 16:58:14 +0000477 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000478
479 memory_object = LocalAlloc( LPTR, bytes_written );
480
Jakob Eriksson43b31772002-05-29 16:58:14 +0000481 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000482
483 contents = LocalLock( memory_object );
484
485 filehandle = _lopen( filename, OF_READ );
486
487 contents = LocalLock( memory_object );
488
Jakob Eriksson43b31772002-05-29 16:58:14 +0000489 ok( NULL != contents, "LocalLock whines" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000490
Jakob Eriksson43b31772002-05-29 16:58:14 +0000491 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000492
493 checksum[0] = '\0';
494 i = 0;
495 do
496 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000497 checksum[0] += contents[i];
Jakob Erikssone8449e62002-05-09 20:29:15 +0000498 i++;
499 }
500 while (i < bytes_written - 1);
501
Jakob Eriksson43b31772002-05-29 16:58:14 +0000502 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
Alexandre Julliard8dc3a512002-05-10 01:10:04 +0000503
Jakob Eriksson43b31772002-05-29 16:58:14 +0000504 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000505
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000506 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000507}
508
Hans Leidekker74992422003-12-08 22:21:20 +0000509static void test_CopyFileA(void)
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000510{
511 char temp_path[MAX_PATH];
512 char source[MAX_PATH], dest[MAX_PATH];
513 static const char prefix[] = "pfx";
514 DWORD ret;
515
516 ret = GetTempPathA(MAX_PATH, temp_path);
517 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
518 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
519
520 ret = GetTempFileNameA(temp_path, prefix, 0, source);
521 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
522
523 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
524 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
525
526 ret = CopyFileA(source, dest, TRUE);
527 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
528 "CopyFileA: unexpected error %ld\n", GetLastError());
529
530 ret = CopyFileA(source, dest, FALSE);
531 ok(ret, "CopyFileA: error %ld\n", GetLastError());
532
533 ret = DeleteFileA(source);
534 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
535 ret = DeleteFileA(dest);
536 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
537}
538
Hans Leidekker74992422003-12-08 22:21:20 +0000539static void test_CopyFileW(void)
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000540{
541 WCHAR temp_path[MAX_PATH];
542 WCHAR source[MAX_PATH], dest[MAX_PATH];
543 static const WCHAR prefix[] = {'p','f','x',0};
544 DWORD ret;
545
546 ret = GetTempPathW(MAX_PATH, temp_path);
Francois Gougete948ad12002-12-12 03:54:01 +0000547 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
548 return;
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000549 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
550 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
551
552 ret = GetTempFileNameW(temp_path, prefix, 0, source);
553 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
554
555 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
556 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
557
558 ret = CopyFileW(source, dest, TRUE);
559 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
560 "CopyFileW: unexpected error %ld\n", GetLastError());
561
562 ret = CopyFileW(source, dest, FALSE);
563 ok(ret, "CopyFileW: error %ld\n", GetLastError());
564
565 ret = DeleteFileW(source);
566 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
567 ret = DeleteFileW(dest);
568 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
569}
570
Hans Leidekker74992422003-12-08 22:21:20 +0000571static void test_CreateFileA(void)
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000572{
573 HANDLE hFile;
574 char temp_path[MAX_PATH];
575 char filename[MAX_PATH];
576 static const char prefix[] = "pfx";
577 DWORD ret;
578
579 ret = GetTempPathA(MAX_PATH, temp_path);
580 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
581 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
582
583 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
584 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
585
586 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
587 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
588 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
589 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
590
591 ret = DeleteFileA(filename);
592 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
593}
594
Hans Leidekker74992422003-12-08 22:21:20 +0000595static void test_CreateFileW(void)
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000596{
597 HANDLE hFile;
598 WCHAR temp_path[MAX_PATH];
599 WCHAR filename[MAX_PATH];
600 static const WCHAR prefix[] = {'p','f','x',0};
601 DWORD ret;
602
603 ret = GetTempPathW(MAX_PATH, temp_path);
Francois Gougete948ad12002-12-12 03:54:01 +0000604 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
605 return;
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000606 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
607 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
608
609 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
610 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
611
612 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
613 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
614 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
615 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
616
617 ret = DeleteFileW(filename);
618 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
619}
620
Mike Hearn517248d2004-01-16 04:49:13 +0000621
622static void test_GetTempFileNameA() {
623 UINT result;
624 char out[MAX_PATH];
625 char *expected = "c:\\windows\\abc2.tmp";
626
627 /* this test may depend on the config file settings */
628 result = GetTempFileNameA("C:", "abc", 1, out);
629 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
630 ok( ((out[0] == 'C') && (out[1] == ':')) && (out[2] == '\\'), "GetTempFileNameA: first three characters should be C:\\, string was actually %s", out );
631
632 result = GetTempFileNameA("c:\\windows\\", "abc", 2, out);
633 ok( result != 0, "GetTempFileNameA: error %ld\n", GetLastError() );
634 ok( strcasecmp( out, expected ) == 0, "GetTempFileNameA: Unexpected output \"%s\" vs \"%s\"\n", out, expected );
635}
636
637
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000638static void test_DeleteFileA( void )
639{
640 BOOL ret;
Francois Gougete948ad12002-12-12 03:54:01 +0000641
642 ret = DeleteFileA(NULL);
643 ok(!ret && (GetLastError() == ERROR_INVALID_PARAMETER ||
644 GetLastError() == ERROR_PATH_NOT_FOUND),
645 "DeleteFileA(NULL) returned ret=%d error=%ld",ret,GetLastError());
646
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000647 ret = DeleteFileA("");
Francois Gougete948ad12002-12-12 03:54:01 +0000648 ok(!ret && (GetLastError() == ERROR_PATH_NOT_FOUND ||
649 GetLastError() == ERROR_BAD_PATHNAME),
650 "DeleteFileA(\"\") returned ret=%d error=%ld",ret,GetLastError());
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000651}
652
Francois Gougete948ad12002-12-12 03:54:01 +0000653static void test_DeleteFileW( void )
654{
655 BOOL ret;
656 WCHAR emptyW[]={'\0'};
657
658 ret = DeleteFileW(NULL);
659 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
660 return;
661 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
662 "DeleteFileW(NULL) returned ret=%d error=%ld",ret,GetLastError());
663
664 ret = DeleteFileW(emptyW);
665 ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND,
666 "DeleteFileW(\"\") returned ret=%d error=%ld",ret,GetLastError());
667}
668
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000669#define IsDotDir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
670
Hans Leidekker74992422003-12-08 22:21:20 +0000671static void test_MoveFileA(void)
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000672{
673 char tempdir[MAX_PATH];
674 char source[MAX_PATH], dest[MAX_PATH];
675 static const char prefix[] = "pfx";
676 DWORD ret;
677
678 ret = GetTempPathA(MAX_PATH, tempdir);
679 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
680 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
681
682 ret = GetTempFileNameA(tempdir, prefix, 0, source);
683 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
684
685 ret = GetTempFileNameA(tempdir, prefix, 0, dest);
686 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
687
688 ret = MoveFileA(source, dest);
Hans Leidekker51367052003-12-15 20:15:20 +0000689 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000690 "MoveFileA: unexpected error %ld\n", GetLastError());
691
692 ret = DeleteFileA(dest);
693 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
694
695 ret = MoveFileA(source, dest);
696 ok(ret, "MoveFileA: failed, error %ld\n", GetLastError());
697
698 lstrcatA(tempdir, "Remove Me");
699 ret = CreateDirectoryA(tempdir, NULL);
700 ok(ret == TRUE, "CreateDirectoryA failed");
701
702 lstrcpyA(source, dest);
703 lstrcpyA(dest, tempdir);
704 lstrcatA(dest, "\\wild?.*");
705 ret = MoveFileA(source, dest);
706 todo_wine {
Hans Leidekker74992422003-12-08 22:21:20 +0000707 ok(!ret, "MoveFileA: shouldn't move to wildcard file");
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000708 ok(GetLastError() == ERROR_INVALID_NAME,
709 "MoveFileA: with wildcards, unexpected error %ld\n", GetLastError());
Hans Leidekker74992422003-12-08 22:21:20 +0000710#if 0
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000711 if (ret || (GetLastError() != ERROR_INVALID_NAME))
Hans Leidekker74992422003-12-08 22:21:20 +0000712 {
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000713 WIN32_FIND_DATAA fd;
714 char temppath[MAX_PATH];
715 HANDLE hFind;
Jon Griffiths90535ce2003-09-25 20:29:40 +0000716
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000717 lstrcpyA(temppath, tempdir);
718 lstrcatA(temppath, "\\*.*");
719 hFind = FindFirstFileA(temppath, &fd);
720 if (INVALID_HANDLE_VALUE != hFind)
Hans Leidekker74992422003-12-08 22:21:20 +0000721 {
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000722 LPSTR lpName;
723 do
Hans Leidekker74992422003-12-08 22:21:20 +0000724 {
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000725 lpName = fd.cAlternateFileName;
726 if (!lpName[0])
727 lpName = fd.cFileName;
Hans Leidekker74992422003-12-08 22:21:20 +0000728 ok(IsDotDir(lpName), "MoveFileA: wildcards file created!");
729 }
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000730 while (FindNextFileA(hFind, &fd));
731 FindClose(hFind);
Hans Leidekker74992422003-12-08 22:21:20 +0000732 }
733 }
734#endif
735 ret = DeleteFileA(source);
736 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
737 ret = DeleteFileA(dest);
738 ok(!ret, "DeleteFileA: error %ld\n", GetLastError());
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000739 }
740
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000741 ret = RemoveDirectoryA(tempdir);
742 ok(ret, "DeleteDirectoryA: error %ld\n", GetLastError());
743}
744
Hans Leidekker74992422003-12-08 22:21:20 +0000745static void test_MoveFileW(void)
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000746{
747 WCHAR temp_path[MAX_PATH];
748 WCHAR source[MAX_PATH], dest[MAX_PATH];
749 static const WCHAR prefix[] = {'p','f','x',0};
750 DWORD ret;
751
752 ret = GetTempPathW(MAX_PATH, temp_path);
753 if (ret==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
754 return;
755 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
756 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
757
758 ret = GetTempFileNameW(temp_path, prefix, 0, source);
759 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
760
761 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
762 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
763
764 ret = MoveFileW(source, dest);
Hans Leidekker51367052003-12-15 20:15:20 +0000765 ok(!ret && GetLastError() == ERROR_ALREADY_EXISTS,
Rolf Kalbermatterd4f13812003-09-16 01:19:21 +0000766 "CopyFileW: unexpected error %ld\n", GetLastError());
767
768 ret = DeleteFileW(source);
769 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
770 ret = DeleteFileW(dest);
771 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
772}
773
Francois Gougete948ad12002-12-12 03:54:01 +0000774#define PATTERN_OFFSET 0x10
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000775
Hans Leidekker74992422003-12-08 22:21:20 +0000776static void test_offset_in_overlapped_structure(void)
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000777{
778 HANDLE hFile;
779 OVERLAPPED ov;
780 DWORD done;
Alexandre Julliardb7a8eef2003-02-11 22:11:56 +0000781 BOOL rc;
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000782 BYTE buf[256], pattern[] = "TeSt";
783 UINT i;
784 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
785
786 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
787 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
788
789 /*** Write File *****************************************************/
790
791 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
792 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
793
794 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
795 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
796 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
797
798 memset(&ov, 0, sizeof(ov));
799 ov.Offset = PATTERN_OFFSET;
800 ov.OffsetHigh = 0;
Alexandre Julliardb7a8eef2003-02-11 22:11:56 +0000801 rc=WriteFile(hFile, pattern, sizeof(pattern), &done, &ov);
802 /* Win 9x does not support the overlapped I/O on files */
803 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
804 ok(rc, "WriteFile error %ld", GetLastError());
805 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
806 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
807 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
808 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000809
Alexandre Julliardb7a8eef2003-02-11 22:11:56 +0000810 ov.Offset = sizeof(buf) * 2;
811 ov.OffsetHigh = 0;
812 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
813 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
814 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
815 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
816 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
817 }
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000818
819 CloseHandle(hFile);
820
821 /*** Read File *****************************************************/
822
823 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
824 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
825
826 memset(buf, 0, sizeof(buf));
827 memset(&ov, 0, sizeof(ov));
828 ov.Offset = PATTERN_OFFSET;
829 ov.OffsetHigh = 0;
Alexandre Julliardb7a8eef2003-02-11 22:11:56 +0000830 rc=ReadFile(hFile, buf, sizeof(pattern), &done, &ov);
831 /* Win 9x does not support the overlapped I/O on files */
832 if (rc || GetLastError()!=ERROR_INVALID_PARAMETER) {
833 ok(rc, "ReadFile error %ld", GetLastError());
834 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
835 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
836 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
837 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
838 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
839 }
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000840
841 CloseHandle(hFile);
842
843 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
844}
Jakob Erikssone8449e62002-05-09 20:29:15 +0000845
Alexandre Julliardce613492003-03-18 05:04:33 +0000846static void test_LockFile(void)
847{
848 HANDLE handle;
849 DWORD written;
850 OVERLAPPED overlapped;
851
852 handle = CreateFileA( filename, GENERIC_READ | GENERIC_WRITE,
853 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
854 CREATE_ALWAYS, 0, 0 );
855 if (handle == INVALID_HANDLE_VALUE)
856 {
857 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
858 return;
859 }
860 ok( WriteFile( handle, sillytext, strlen(sillytext), &written, NULL ), "write failed" );
861
862 ok( LockFile( handle, 0, 0, 0, 0 ), "LockFile failed" );
863 ok( UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile failed" );
864 ok( !UnlockFile( handle, 0, 0, 0, 0 ), "UnlockFile succeeded" );
865
866 ok( LockFile( handle, 10, 0, 20, 0 ), "LockFile 10,20 failed" );
867 /* overlapping locks must fail */
868 ok( !LockFile( handle, 12, 0, 10, 0 ), "LockFile 12,10 succeeded" );
869 ok( !LockFile( handle, 5, 0, 6, 0 ), "LockFile 5,6 succeeded" );
870 /* non-overlapping locks must succeed */
871 ok( LockFile( handle, 5, 0, 5, 0 ), "LockFile 5,5 failed" );
872
873 ok( !UnlockFile( handle, 10, 0, 10, 0 ), "UnlockFile 10,10 succeeded" );
874 ok( UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 failed" );
875 ok( !UnlockFile( handle, 10, 0, 20, 0 ), "UnlockFile 10,20 again succeeded" );
876 ok( UnlockFile( handle, 5, 0, 5, 0 ), "UnlockFile 5,5 failed" );
877
878 overlapped.Offset = 100;
879 overlapped.OffsetHigh = 0;
880 overlapped.hEvent = 0;
881 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 100,100 failed" );
882 /* overlapping shared locks are OK */
883 overlapped.Offset = 150;
884 ok( LockFileEx( handle, 0, 0, 100, 0, &overlapped ), "LockFileEx 150,100 failed" );
885 /* but exclusive is not */
886 ok( !LockFileEx( handle, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, 50, 0, &overlapped ),
887 "LockFileEx exclusive 150,50 succeeded" );
888 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 failed" );
889 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 150,100 again succeeded" );
890 overlapped.Offset = 100;
891 ok( UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 failed" );
892 ok( !UnlockFileEx( handle, 0, 100, 0, &overlapped ), "UnlockFileEx 100,100 again succeeded" );
893
894 ok( LockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "LockFile failed" );
895 ok( !LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 succeeded" );
896 ok( !LockFile( handle, 0, 0x20000000, 20, 0 ), "LockFile 0x20000000,20 succeeded" );
897 ok( UnlockFile( handle, 0, 0x10000000, 0, 0xf0000000 ), "UnlockFile failed" );
898
899 /* wrap-around lock should not do anything */
900 /* (but still succeeds on NT4 so we don't check result) */
901 LockFile( handle, 0, 0x10000000, 0, 0xf0000001 );
902 ok( LockFile( handle, ~0, ~0, 1, 0 ), "LockFile ~0,1 failed" );
903 ok( UnlockFile( handle, ~0, ~0, 1, 0 ), "Unlockfile ~0,1 failed" );
904
905 /* zero-byte lock */
906 ok( LockFile( handle, 100, 0, 0, 0 ), "LockFile 100,0 failed" );
907 ok( !LockFile( handle, 98, 0, 4, 0 ), "LockFile 98,4 succeeded" );
908 ok( LockFile( handle, 90, 0, 10, 0 ), "LockFile 90,10 failed" );
909 ok( LockFile( handle, 100, 0, 10, 0 ), "LockFile 100,10 failed" );
910 ok( UnlockFile( handle, 90, 0, 10, 0 ), "UnlockFile 90,10 failed" );
911 ok( UnlockFile( handle, 100, 0, 10, 0 ), "UnlockFile 100,10 failed" );
912 ok( UnlockFile( handle, 100, 0, 0, 0 ), "UnlockFile 100,0 failed" );
913
914 CloseHandle( handle );
915 DeleteFileA( filename );
916}
917
Hans Leidekker74992422003-12-08 22:21:20 +0000918static void test_FindFirstFileA()
Uwe Bonnes0de21b62003-04-17 02:10:04 +0000919{
920 HANDLE handle;
921 WIN32_FIND_DATAA search_results;
922 int err;
923
Uwe Bonnes0de21b62003-04-17 02:10:04 +0000924 handle = FindFirstFileA("C:\\",&search_results);
925 err = GetLastError();
926 ok ( handle == INVALID_HANDLE_VALUE , "FindFirstFile on Root directory should Fail");
927 if (handle == INVALID_HANDLE_VALUE)
Ferenc Wagner4b28f0f2003-10-21 23:42:50 +0000928 ok ( err == ERROR_FILE_NOT_FOUND, "Bad Error number %d\n", err);
Uwe Bonnes0de21b62003-04-17 02:10:04 +0000929 handle = FindFirstFileA("C:\\*",&search_results);
930 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
931 ok ( FindClose(handle) == TRUE, "Failed to close handle");
932}
Alexandre Julliardce613492003-03-18 05:04:33 +0000933
Hans Leidekker74992422003-12-08 22:21:20 +0000934static void test_FindNextFileA()
Alex Pasadyn33854af2003-09-16 01:22:05 +0000935{
936 HANDLE handle;
937 WIN32_FIND_DATAA search_results;
938 int err;
939
940 handle = FindFirstFileA("C:\\*",&search_results);
941 ok ( handle != INVALID_HANDLE_VALUE, "FindFirstFile on C:\\* should succeed" );
942 while (FindNextFile(handle, &search_results))
943 {
944 /* get to the end of the files */
945 }
946 ok ( FindClose(handle) == TRUE, "Failed to close handle");
947 err = GetLastError();
948 ok ( err == ERROR_NO_MORE_FILES, "GetLastError should return ERROR_NO_MORE_FILES");
949}
950
Mike McCormackf7b47f72004-01-09 00:34:37 +0000951static void test_MapFile()
952{
953 HANDLE handle, hmap;
954
955 /* be sure to remove stale files */
956 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL);
957 DeleteFile(filename);
958 handle = CreateFile( filename, GENERIC_READ|GENERIC_WRITE, 0, 0,
959 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
960 ok( handle != INVALID_HANDLE_VALUE, "couldn't create test file\n");
961
962 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0, NULL );
963 ok( hmap == NULL, "mapped zero size file\n");
964 ok( GetLastError() == ERROR_FILE_INVALID, "not ERROR_FILE_INVALID\n");
965
966 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0, NULL );
967 ok( hmap == NULL, "mapping should fail\n");
968 /* GetLastError() varies between win9x and WinNT */
969
970 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0x1000, 0x10000, NULL );
971 ok( hmap == NULL, "mapping should fail\n");
972 /* GetLastError() varies between win9x and WinNT */
973
974 hmap = CreateFileMapping( handle, NULL, PAGE_READWRITE, 0, 0x1000, NULL );
975 ok( hmap != NULL, "mapping should succeed\n");
976
977 ok( CloseHandle( hmap ), "can't close mapping handle\n");
978 ok( CloseHandle( handle ), "can't close file handle\n");
979 ok( DeleteFileA( filename ), "DeleteFile failed after map" );
980}
981
Jakob Eriksson33151992002-04-29 17:14:50 +0000982START_TEST(file)
983{
984 test__hread( );
985 test__hwrite( );
986 test__lclose( );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000987 test__lcreat( );
988 test__llseek( );
989 test__llopen( );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000990 test__lread( );
991 test__lwrite( );
Mike Hearn517248d2004-01-16 04:49:13 +0000992 test_GetTempFileNameA();
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000993 test_CopyFileA();
994 test_CopyFileW();
995 test_CreateFileA();
996 test_CreateFileW();
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000997 test_DeleteFileA();
Francois Gougete948ad12002-12-12 03:54:01 +0000998 test_DeleteFileW();
Hans Leidekker74992422003-12-08 22:21:20 +0000999 test_MoveFileA();
1000 test_MoveFileW();
Uwe Bonnes0de21b62003-04-17 02:10:04 +00001001 test_FindFirstFileA();
Alex Pasadyn33854af2003-09-16 01:22:05 +00001002 test_FindNextFileA();
Alexandre Julliardce613492003-03-18 05:04:33 +00001003 test_LockFile();
Dmitry Timoshkov160ab192002-08-27 01:56:08 +00001004 test_offset_in_overlapped_structure();
Mike McCormackf7b47f72004-01-09 00:34:37 +00001005 test_MapFile();
Jakob Eriksson33151992002-04-29 17:14:50 +00001006}