2 * Unit tests for file functions in Wine
4 * Copyright (c) 2002 Jakob Eriksson
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.
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.
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
25 #include "wine/test.h"
30 LPCSTR filename = "testfile.xxx";
32 "en larvig liten text dx \033 gx hej 84 hej 4484 ! \001\033 bla bl\na.. bla bla."
33 "1234 43 4kljf lf &%%%&&&&&& 34 4 34 3############# 33 3 3 3 # 3## 3"
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 "sdlkfjasdlkfj a dslkj adsklf \n \nasdklf askldfa sdlkf \nsadklf asdklf asdf ";
44 static void test__hread( void )
52 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
53 DeleteFileA( filename );
54 filehandle = _lcreat( filename, 0 );
55 if (filehandle == HFILE_ERROR)
57 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
61 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
63 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
65 filehandle = _lopen( filename, OF_READ );
67 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)", filename, GetLastError( ) );
69 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
71 ok( strlen( sillytext ) == bytes_read, "file read size error" );
73 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
75 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
76 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
77 for (i = 0; i < bytes_wanted; i++)
79 ok( buffer[i] == sillytext[i], "that's not what's written" );
83 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
85 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
89 static void test__hwrite( void )
101 filehandle = _lcreat( filename, 0 );
102 if (filehandle == HFILE_ERROR)
104 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
108 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
110 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
112 filehandle = _lopen( filename, OF_READ );
114 bytes_read = _hread( filehandle, buffer, 1);
116 ok( 0 == bytes_read, "file read size error" );
118 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
120 filehandle = _lopen( filename, OF_READWRITE );
124 srand( (unsigned)time( NULL ) );
125 for (blocks = 0; blocks < 100; blocks++)
127 for (i = 0; i < sizeof( buffer ); i++)
130 checksum[0] = checksum[0] + buffer[i];
132 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
133 bytes_written = bytes_written + sizeof( buffer );
136 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
139 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
141 memory_object = LocalAlloc( LPTR, bytes_written );
143 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
145 contents = LocalLock( memory_object );
147 filehandle = _lopen( filename, OF_READ );
149 contents = LocalLock( memory_object );
151 ok( NULL != contents, "LocalLock whines" );
153 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
159 checksum[0] = checksum[0] + contents[i];
162 while (i < bytes_written - 1);
164 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
166 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
168 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
172 static void test__lclose( void )
176 filehandle = _lcreat( filename, 0 );
177 if (filehandle == HFILE_ERROR)
179 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
183 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
185 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
187 ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this" );
189 ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this" );
191 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
195 static void test__lcreat( void )
199 WIN32_FIND_DATAA search_results;
201 filehandle = _lcreat( filename, 0 );
202 if (filehandle == HFILE_ERROR)
204 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
208 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
210 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
212 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( sillytext ), "erratic _hread return value" );
214 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
216 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
218 ok( DeleteFileA(filename) != 0, "DeleteFile failed (%d)", GetLastError());
220 filehandle = _lcreat( filename, 1 ); /* readonly */
221 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)", filename, GetLastError( ) );
223 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less" );
225 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
227 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
229 ok( 0 == DeleteFileA( filename ), "shouldn't be able to delete a readonly file" );
231 ok( SetFileAttributesA(filename, FILE_ATTRIBUTE_NORMAL ) != 0, "couldn't change attributes on file" );
233 ok( DeleteFileA( filename ) != 0, "now it should be possible to delete the file!" );
235 filehandle = _lcreat( filename, 2 );
236 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)", filename, GetLastError( ) );
238 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
240 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
242 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( sillytext ), "erratic _hread return value" );
244 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
246 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
248 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
250 filehandle = _lcreat( filename, 4 ); /* SYSTEM file */
251 ok( HFILE_ERROR != filehandle, "couldn't create file \"%s\" (err=%d)", filename, GetLastError( ) );
253 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
255 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
257 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( sillytext ), "erratic _hread return value" );
259 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
261 ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should STILL be able to find file" );
263 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
267 void test__llseek( void )
274 filehandle = _lcreat( filename, 0 );
275 if (filehandle == HFILE_ERROR)
277 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
281 for (i = 0; i < 400; i++)
283 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
285 ok( HFILE_ERROR != _llseek( filehandle, 400 * strlen( sillytext ), FILE_CURRENT ), "should be able to seek" );
286 ok( HFILE_ERROR != _llseek( filehandle, 27 + 35 * strlen( sillytext ), FILE_BEGIN ), "should be able to seek" );
288 bytes_read = _hread( filehandle, buffer, 1);
289 ok( 1 == bytes_read, "file read size error" );
290 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
291 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
293 bytes_read = _hread( filehandle, buffer, 1);
294 ok( 1 == bytes_read, "file read size error" );
295 ok( buffer[0] == sillytext[0], "_llseek error, it got lost seeking" );
296 ok( HFILE_ERROR != _llseek( filehandle, 1000000, FILE_END ), "should be able to seek past file; poor, poor Windows programmers" );
297 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
299 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
303 static void test__llopen( void )
309 filehandle = _lcreat( filename, 0 );
310 if (filehandle == HFILE_ERROR)
312 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
316 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
317 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
319 filehandle = _lopen( filename, OF_READ );
320 ok( HFILE_ERROR == _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write!" );
321 bytes_read = _hread( filehandle, buffer, strlen( sillytext ) );
322 ok( strlen( sillytext ) == bytes_read, "file read size error" );
323 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
325 filehandle = _lopen( filename, OF_READWRITE );
326 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
327 ok( strlen( sillytext ) == bytes_read, "file read size error" );
328 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
329 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
331 filehandle = _lopen( filename, OF_WRITE );
332 ok( HFILE_ERROR == _hread( filehandle, buffer, 1 ), "you should only be able to write this file" );
333 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite should write just fine" );
334 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
336 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
337 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
341 static void test__lread( void )
349 filehandle = _lcreat( filename, 0 );
350 if (filehandle == HFILE_ERROR)
352 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
356 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
358 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
360 filehandle = _lopen( filename, OF_READ );
362 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%d)", filename, GetLastError());
364 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
366 ok( strlen( sillytext ) == bytes_read, "file read size error" );
368 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
370 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
371 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
372 for (i = 0; i < bytes_wanted; i++)
374 ok( buffer[i] == sillytext[i], "that's not what's written" );
378 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
380 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );
384 static void test__lwrite( void )
393 HLOCAL memory_object;
396 filehandle = _lcreat( filename, 0 );
397 if (filehandle == HFILE_ERROR)
399 ok(0,"couldn't create file \"%s\" (err=%d)",filename,GetLastError());
403 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
405 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
407 filehandle = _lopen( filename, OF_READ );
409 bytes_read = _hread( filehandle, buffer, 1);
411 ok( 0 == bytes_read, "file read size error" );
413 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
415 filehandle = _lopen( filename, OF_READWRITE );
419 srand( (unsigned)time( NULL ) );
420 for (blocks = 0; blocks < 100; blocks++)
422 for (i = 0; i < sizeof( buffer ); i++)
425 checksum[0] = checksum[0] + buffer[i];
427 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
428 bytes_written = bytes_written + sizeof( buffer );
431 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
434 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
436 memory_object = LocalAlloc( LPTR, bytes_written );
438 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
440 contents = LocalLock( memory_object );
442 filehandle = _lopen( filename, OF_READ );
444 contents = LocalLock( memory_object );
446 ok( NULL != contents, "LocalLock whines" );
448 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
454 checksum[0] += contents[i];
457 while (i < bytes_written - 1);
459 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
461 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
463 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%d)", GetLastError( ) );