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
24 #include "wine/test.h"
31 LPCSTR filename
= "testfile.xxx";
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 ";
45 static void test__hread( void )
53 filehandle
= _lcreat( filename
, 0 );
54 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
56 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
58 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
60 filehandle
= _lopen( filename
, OF_READ
);
62 ok( HFILE_ERROR
!= filehandle
, "couldn't open file again?");
64 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
66 ok( strlen( sillytext
) == bytes_read
, "file read size error." );
68 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
70 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains." );
71 ok( _hread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value." );
72 for (i
= 0; i
< bytes_wanted
; i
++)
74 ok( buffer
[i
] == sillytext
[i
], "that's not what's written." );
78 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
80 ok( DeleteFileA( filename
) != 0, "DeleteFile complains." );
84 static void test__hwrite( void )
96 filehandle
= _lcreat( filename
, 0 );
97 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
99 ok( HFILE_ERROR
!= _hwrite( filehandle
, "", 0 ), "_hwrite complains." );
101 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
103 filehandle
= _lopen( filename
, OF_READ
);
105 bytes_read
= _hread( filehandle
, buffer
, 1);
107 ok( 0 == bytes_read
, "file read size error." );
109 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
111 filehandle
= _lopen( filename
, OF_READWRITE
);
115 srand( (unsigned)time( NULL
) );
116 for (blocks
= 0; blocks
< 100; blocks
++)
118 for (i
= 0; i
< sizeof( buffer
); i
++)
121 checksum
[0] = checksum
[0] + buffer
[i
];
123 ok( HFILE_ERROR
!= _hwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains." );
124 bytes_written
= bytes_written
+ sizeof( buffer
);
127 ok( HFILE_ERROR
!= _hwrite( filehandle
, checksum
, 1 ), "_hwrite complains." );
130 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
132 memory_object
= LocalAlloc( LPTR
, bytes_written
);
134 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)" );
136 contents
= LocalLock( memory_object
);
138 filehandle
= _lopen( filename
, OF_READ
);
140 contents
= LocalLock( memory_object
);
142 ok( NULL
!= contents
, "LocalLock whines." );
144 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length." );
150 checksum
[0] = checksum
[0] + contents
[i
];
153 while (i
< bytes_written
- 1);
155 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum." );
157 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
159 ok( DeleteFileA( filename
) != 0, "DeleteFile complains." );
163 static void test__lclose( void )
167 filehandle
= _lcreat( filename
, 0 );
168 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
170 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
172 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
174 ok( HFILE_ERROR
== _lclose(filehandle
), "_lclose should whine about this." );
176 ok( HFILE_ERROR
== _lclose(filehandle
), "_lclose should whine about this." );
178 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
182 static void test__lcreat( void )
186 WIN32_FIND_DATAA search_results
;
188 filehandle
= _lcreat( filename
, 0 );
189 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
191 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
193 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains." );
195 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == strlen( sillytext
), "erratic _hread return value." );
197 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
199 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file" );
201 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
203 filehandle
= _lcreat( filename
, 1 );
204 ok( HFILE_ERROR
!= filehandle
, "couldn't create file!?" );
206 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write never the less." );
208 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
210 ok( INVALID_HANDLE_VALUE
!= FindFirstFileA( filename
, &search_results
), "should be able to find file" );
212 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
215 filehandle
= _lcreat( filename
, 2 );
216 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
218 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
220 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains." );
222 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == strlen( sillytext
), "erratic _hread return value." );
224 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
228 ok( INVALID_HANDLE_VALUE
== FindFirstFileA( filename
, &search_results
), "should NOT be able to find file" );
231 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
233 filehandle
= _lcreat( filename
, 4 );
234 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
236 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
238 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains." );
240 ok( _hread( filehandle
, buffer
, strlen( sillytext
) ) == strlen( sillytext
), "erratic _hread return value." );
242 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
246 ok( INVALID_HANDLE_VALUE
== FindFirstFileA( filename
, &search_results
), "should NOT be able to find file" );
249 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
253 void test__llseek( void )
260 filehandle
= _lcreat( filename
, 0 );
262 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
263 for (i
= 0; i
< 400; i
++)
265 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
267 ok( HFILE_ERROR
!= _llseek( filehandle
, 400 * strlen( sillytext
), FILE_CURRENT
), "should be able to seek" );
268 ok( HFILE_ERROR
!= _llseek( filehandle
, 27 + 35 * strlen( sillytext
), FILE_BEGIN
), "should be able to seek" );
270 bytes_read
= _hread( filehandle
, buffer
, 1);
271 ok( 1 == bytes_read
, "file read size error." );
272 ok( buffer
[0] == sillytext
[27], "_llseek error. It got lost seeking..." );
273 ok( HFILE_ERROR
!= _llseek( filehandle
, -400 * strlen( sillytext
), FILE_END
), "should be able to seek" );
275 bytes_read
= _hread( filehandle
, buffer
, 1);
276 ok( 1 == bytes_read
, "file read size error." );
277 ok( buffer
[0] == sillytext
[0], "_llseek error. It got lost seeking..." );
278 ok( HFILE_ERROR
!= _llseek( filehandle
, 1000000, FILE_END
), "should be able to seek past file. Poor, poor Windows programmers." );
279 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
281 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
285 static void test__llopen( void )
291 filehandle
= _lcreat( filename
, 0 );
292 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
293 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
294 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
296 filehandle
= _lopen( filename
, OF_READ
);
297 ok( HFILE_ERROR
== _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite shouldn't be able to write!" );
298 bytes_read
= _hread( filehandle
, buffer
, strlen( sillytext
) );
299 ok( strlen( sillytext
) == bytes_read
, "file read size error." );
300 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
302 filehandle
= _lopen( filename
, OF_READWRITE
);
303 bytes_read
= _hread( filehandle
, buffer
, 2 * strlen( sillytext
) );
304 ok( strlen( sillytext
) == bytes_read
, "file read size error." );
305 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine." );
306 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
308 filehandle
= _lopen( filename
, OF_WRITE
);
309 ok( HFILE_ERROR
== _hread( filehandle
, buffer
, 1 ), "you should only be able to write this file..." );
310 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite should write just fine." );
311 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
313 ok( DeleteFileA( filename
) != 0, "DeleteFileA complains." );
314 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
318 static void test__lread( void )
326 filehandle
= _lcreat( filename
, 0 );
327 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
329 ok( HFILE_ERROR
!= _hwrite( filehandle
, sillytext
, strlen( sillytext
) ), "_hwrite complains." );
331 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
333 filehandle
= _lopen( filename
, OF_READ
);
335 ok( HFILE_ERROR
!= filehandle
, "couldn't open file again?");
337 bytes_read
= _lread( filehandle
, buffer
, 2 * strlen( sillytext
) );
339 ok( strlen( sillytext
) == bytes_read
, "file read size error." );
341 for (bytes_wanted
= 0; bytes_wanted
< strlen( sillytext
); bytes_wanted
++)
343 ok( 0 == _llseek( filehandle
, 0, FILE_BEGIN
), "_llseek complains." );
344 ok( _lread( filehandle
, buffer
, bytes_wanted
) == bytes_wanted
, "erratic _hread return value." );
345 for (i
= 0; i
< bytes_wanted
; i
++)
347 ok( buffer
[i
] == sillytext
[i
], "that's not what's written." );
351 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
353 ok( DeleteFileA( filename
) != 0, "DeleteFile complains." );
357 static void test__lwrite( void )
366 HLOCAL memory_object
;
369 filehandle
= _lcreat( filename
, 0 );
370 ok( HFILE_ERROR
!= filehandle
, "couldn't create file. Wrong permissions on directory?" );
372 ok( HFILE_ERROR
!= _lwrite( filehandle
, "", 0 ), "_hwrite complains." );
374 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
376 filehandle
= _lopen( filename
, OF_READ
);
378 bytes_read
= _hread( filehandle
, buffer
, 1);
380 ok( 0 == bytes_read
, "file read size error." );
382 ok( HFILE_ERROR
!= _lclose(filehandle
), "_lclose complains." );
384 filehandle
= _lopen( filename
, OF_READWRITE
);
388 srand( (unsigned)time( NULL
) );
389 for (blocks
= 0; blocks
< 100; blocks
++)
391 for (i
= 0; i
< sizeof( buffer
); i
++)
394 checksum
[0] = checksum
[0] + buffer
[i
];
396 ok( HFILE_ERROR
!= _lwrite( filehandle
, buffer
, sizeof( buffer
) ), "_hwrite complains." );
397 bytes_written
= bytes_written
+ sizeof( buffer
);
400 ok( HFILE_ERROR
!= _lwrite( filehandle
, checksum
, 1 ), "_hwrite complains." );
403 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
405 memory_object
= LocalAlloc( LPTR
, bytes_written
);
407 ok( 0 != memory_object
, "LocalAlloc fails. (Could be out of memory.)" );
409 contents
= LocalLock( memory_object
);
411 filehandle
= _lopen( filename
, OF_READ
);
413 contents
= LocalLock( memory_object
);
415 ok( NULL
!= contents
, "LocalLock whines." );
417 ok( bytes_written
== _hread( filehandle
, contents
, bytes_written
), "read length differ from write length." );
423 checksum
[0] = checksum
[0] + contents
[i
];
426 while (i
< bytes_written
- 1);
428 ok( checksum
[0] == contents
[i
], "stored checksum differ from computed checksum." );
431 ok( HFILE_ERROR
!= _lclose( filehandle
), "_lclose complains." );
433 ok( DeleteFileA( filename
) != 0, "DeleteFile complains." );