Better implementation of GetCalendarInfo{A,W}, not perfect.
[wine] / dlls / kernel / tests / file.c
1 /*
2  * Unit tests for file functions in Wine
3  *
4  * Copyright (c) 2002 Jakob Eriksson
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "winbase.h"
23 #include "winerror.h"
24 #include "wine/test.h"
25
26
27 #include <stdlib.h>
28 #include <time.h>
29
30
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 ";
43
44
45 static void test__hread( void )
46 {
47     HFILE filehandle;
48     char buffer[10000];
49     long bytes_read;
50     long bytes_wanted;
51     UINT i;
52
53     filehandle = _lcreat( filename, 0 );
54     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
55
56     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
57
58     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
59
60     filehandle = _lopen( filename, OF_READ );
61
62     ok( HFILE_ERROR != filehandle, "couldn't open file again?");
63     
64     bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
65     
66     ok( strlen( sillytext ) == bytes_read, "file read size error." );
67
68     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
69     {
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++)
73         {
74             ok( buffer[i] == sillytext[i], "that's not what's written." );
75         }
76     }
77
78     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
79
80     ok( DeleteFileA( filename ) != 0, "DeleteFile complains." );
81 }
82
83
84 static void test__hwrite( void )
85 {
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];
95
96     filehandle = _lcreat( filename, 0 );
97     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
98
99     ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains." );
100
101     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
102
103     filehandle = _lopen( filename, OF_READ );
104     
105     bytes_read = _hread( filehandle, buffer, 1);
106
107     ok( 0 == bytes_read, "file read size error." );
108
109     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
110
111     filehandle = _lopen( filename, OF_READWRITE );
112
113     bytes_written = 0;
114     checksum[0] = '\0';
115     srand( (unsigned)time( NULL ) );
116     for (blocks = 0; blocks < 100; blocks++)
117     {
118         for (i = 0; i < sizeof( buffer ); i++)
119         {
120             buffer[i] = rand(  );
121             checksum[0] = checksum[0] + buffer[i];
122         }
123         ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains." );
124         bytes_written = bytes_written + sizeof( buffer );
125     }
126
127     ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains." );
128     bytes_written++;
129
130     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
131
132     memory_object = LocalAlloc( LPTR, bytes_written );
133
134     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
135
136     contents = LocalLock( memory_object );
137
138     filehandle = _lopen( filename, OF_READ );
139
140     contents = LocalLock( memory_object );
141
142     ok( NULL != contents, "LocalLock whines." );
143
144     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length." );
145
146     checksum[0] = '\0';
147     i = 0;
148     do
149     {
150         checksum[0] = checksum[0] + contents[i];
151         i++;
152     }
153     while (i < bytes_written - 1);
154
155     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum." );
156
157     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
158
159     ok( DeleteFileA( filename ) != 0, "DeleteFile complains." );
160 }
161
162
163 static void test__lclose( void )
164 {
165     HFILE filehandle;
166
167     filehandle = _lcreat( filename, 0 );
168     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
169
170     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
171
172     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
173
174     ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this." );
175
176     ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this." );
177
178     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
179 }
180
181
182 static void test__lcreat( void )
183 {
184     HFILE filehandle;
185     char buffer[10000];
186     WIN32_FIND_DATAA search_results;
187
188     filehandle = _lcreat( filename, 0 );
189     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
190
191     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
192
193     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains." );
194
195     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  strlen( sillytext ), "erratic _hread return value." );
196
197     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
198
199     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
200
201     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
202
203     filehandle = _lcreat( filename, 1 );
204     ok( HFILE_ERROR != filehandle, "couldn't create file!?" );
205
206     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite shouldn't be able to write never the less." );
207
208     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
209
210     ok( INVALID_HANDLE_VALUE != FindFirstFileA( filename, &search_results ), "should be able to find file" );
211
212     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
213
214
215     filehandle = _lcreat( filename, 2 );
216     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
217
218     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
219
220     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains." );
221
222     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  strlen( sillytext ), "erratic _hread return value." );
223
224     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
225
226     todo_wine
227     {
228         ok( INVALID_HANDLE_VALUE == FindFirstFileA( filename, &search_results ), "should NOT be able to find file" );
229     }
230
231     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
232     
233     filehandle = _lcreat( filename, 4 );
234     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
235
236     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
237
238     ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains." );
239
240     ok( _hread( filehandle, buffer, strlen( sillytext ) ) ==  strlen( sillytext ), "erratic _hread return value." );
241
242     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
243
244     todo_wine
245     {
246         ok( INVALID_HANDLE_VALUE == FindFirstFileA( filename, &search_results ), "should NOT be able to find file" );
247     }
248
249     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
250 }
251
252
253 void test__llseek( void )
254 {
255     INT i;
256     HFILE filehandle;
257     char buffer[1];
258     long bytes_read;
259
260     filehandle = _lcreat( filename, 0 );
261
262     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
263     for (i = 0; i < 400; i++)
264     {
265         ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
266     }
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" );
269
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" );
274
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." );
280
281     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
282 }
283
284
285 static void test__llopen( void )
286 {
287     HFILE filehandle;
288     UINT bytes_read;
289     char buffer[10000];
290
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." );
295
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." );
301     
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." );
307
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." );
312
313     ok( DeleteFileA( filename ) != 0, "DeleteFileA complains." );
314     /* TODO - add tests for the SHARE modes  -  use two processes to pull this one off */
315 }
316
317
318 static void test__lread( void )
319 {
320     HFILE filehandle;
321     char buffer[10000];
322     long bytes_read;
323     UINT bytes_wanted;
324     UINT i;
325
326     filehandle = _lcreat( filename, 0 );
327     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
328
329     ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains." );
330
331     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
332
333     filehandle = _lopen( filename, OF_READ );
334
335     ok( HFILE_ERROR != filehandle, "couldn't open file again?");
336     
337     bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
338     
339     ok( strlen( sillytext ) == bytes_read, "file read size error." );
340
341     for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
342     {
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++)
346         {
347             ok( buffer[i] == sillytext[i], "that's not what's written." );
348         }
349     }
350
351     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
352
353     ok( DeleteFileA( filename ) != 0, "DeleteFile complains." );
354 }
355
356
357 static void test__lwrite( void )
358 {
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];
368
369     filehandle = _lcreat( filename, 0 );
370     ok( HFILE_ERROR != filehandle, "couldn't create file. Wrong permissions on directory?" );
371
372     ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains." );
373
374     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
375
376     filehandle = _lopen( filename, OF_READ );
377     
378     bytes_read = _hread( filehandle, buffer, 1);
379
380     ok( 0 == bytes_read, "file read size error." );
381
382     ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains." );
383
384     filehandle = _lopen( filename, OF_READWRITE );
385
386     bytes_written = 0;
387     checksum[0] = '\0';
388     srand( (unsigned)time( NULL ) );
389     for (blocks = 0; blocks < 100; blocks++)
390     {
391         for (i = 0; i < sizeof( buffer ); i++)
392         {
393             buffer[i] = rand(  );
394             checksum[0] = checksum[0] + buffer[i];
395         }
396         ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains." );
397         bytes_written = bytes_written + sizeof( buffer );
398     }
399
400     ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains." );
401     bytes_written++;
402
403     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
404
405     memory_object = LocalAlloc( LPTR, bytes_written );
406
407     ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
408
409     contents = LocalLock( memory_object );
410
411     filehandle = _lopen( filename, OF_READ );
412
413     contents = LocalLock( memory_object );
414
415     ok( NULL != contents, "LocalLock whines." );
416
417     ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length." );
418
419     checksum[0] = '\0';
420     i = 0;
421     do
422     {
423         checksum[0] = checksum[0] + contents[i];
424         i++;
425     }
426     while (i < bytes_written - 1);
427
428     ok( checksum[0] == contents[i], "stored checksum differ from computed checksum." );
429
430     ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains." );
431
432     ok( DeleteFileA( filename ) != 0, "DeleteFile complains." );
433 }
434
435
436 START_TEST(file)
437 {
438     test__hread(  );
439     test__hwrite(  );
440     test__lclose(  );
441     test__lcreat(  );
442     test__llseek(  );
443     test__llopen(  );
444     test__lread(  );
445     test__lwrite(  );
446 }