Added some more tests.
[wine/gsoc_dplay.git] / dlls / kernel / tests / file.c
blob424b20dd9027bc0d119e36c4d7b37aa7aaf2b00c
1 /*
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
22 #include "winbase.h"
23 #include "winerror.h"
24 #include "wine/test.h"
27 #include <stdlib.h>
28 #include <time.h>
31 LPCSTR filename = "testfile.xxx";
32 LPCSTR 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 ";
45 static void test__hread( void )
47 HFILE filehandle;
48 char buffer[10000];
49 long bytes_read;
50 long bytes_wanted;
51 UINT i;
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 )
86 HFILE filehandle;
87 char buffer[10000];
88 long bytes_read;
89 UINT bytes_written;
90 UINT blocks;
91 UINT i;
92 char *contents;
93 HLOCAL memory_object;
94 char checksum[1];
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 );
113 bytes_written = 0;
114 checksum[0] = '\0';
115 srand( (unsigned)time( NULL ) );
116 for (blocks = 0; blocks < 100; blocks++)
118 for (i = 0; i < sizeof( buffer ); i++)
120 buffer[i] = rand( );
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." );
128 bytes_written++;
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." );
146 checksum[0] = '\0';
147 i = 0;
150 checksum[0] = checksum[0] + contents[i];
151 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 )
165 HFILE filehandle;
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 )
184 HFILE filehandle;
185 char buffer[10000];
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." );
226 todo_wine
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." );
244 todo_wine
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 )
255 INT i;
256 HFILE filehandle;
257 char buffer[1];
258 long bytes_read;
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 )
287 HFILE filehandle;
288 UINT bytes_read;
289 char buffer[10000];
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 )
320 HFILE filehandle;
321 char buffer[10000];
322 long bytes_read;
323 UINT bytes_wanted;
324 UINT i;
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 )
359 HFILE filehandle;
360 char buffer[10000];
361 long bytes_read;
362 UINT bytes_written;
363 UINT blocks;
364 UINT i;
365 char *contents;
366 HLOCAL memory_object;
367 char checksum[1];
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 );
386 bytes_written = 0;
387 checksum[0] = '\0';
388 srand( (unsigned)time( NULL ) );
389 for (blocks = 0; blocks < 100; blocks++)
391 for (i = 0; i < sizeof( buffer ); i++)
393 buffer[i] = rand( );
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." );
401 bytes_written++;
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." );
419 checksum[0] = '\0';
420 i = 0;
423 checksum[0] = checksum[0] + contents[i];
424 i++;
426 while (i < bytes_written - 1);
428 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum." );
429 return;
431 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
433 ok( DeleteFileA( filename ) != 0, "DeleteFile complains." );
437 START_TEST(file)
439 test__hread( );
440 test__hwrite( );
441 test__lclose( );
442 test__lcreat( );
443 test__llseek( );
444 test__llopen( );
445 test__lread( );
446 test__lwrite( );