blob: 7250b8f401d6c9d8bc4ac732af8d5f5217a14e1e [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
Jakob Eriksson33151992002-04-29 17:14:50 +000022#include <stdlib.h>
23#include <time.h>
24
Francois Gouget4c314002002-05-23 02:40:07 +000025#include "wine/test.h"
26#include "winbase.h"
27#include "winerror.h"
28
Jakob Eriksson33151992002-04-29 17:14:50 +000029
30LPCSTR filename = "testfile.xxx";
31LPCSTR sillytext =
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 ";
42
Jakob Erikssone8449e62002-05-09 20:29:15 +000043
Jakob Eriksson33151992002-04-29 17:14:50 +000044static void test__hread( void )
45{
46 HFILE filehandle;
47 char buffer[10000];
48 long bytes_read;
Jakob Erikssone8449e62002-05-09 20:29:15 +000049 long bytes_wanted;
Jakob Eriksson33151992002-04-29 17:14:50 +000050 UINT i;
51
Alexandre Julliard034e39b2002-06-05 00:47:38 +000052 SetFileAttributesA(filename,FILE_ATTRIBUTE_NORMAL); /* be sure to remove stale files */
53 DeleteFileA( filename );
Jakob Eriksson33151992002-04-29 17:14:50 +000054 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +000055 if (filehandle == HFILE_ERROR)
56 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000057 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +000058 return;
59 }
Jakob Eriksson33151992002-04-29 17:14:50 +000060
Jakob Eriksson43b31772002-05-29 16:58:14 +000061 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000062
Jakob Eriksson43b31772002-05-29 16:58:14 +000063 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000064
65 filehandle = _lopen( filename, OF_READ );
Jakob Erikssonb02f0942002-05-06 20:12:55 +000066
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000067 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError( ) );
Francois Gougetc9493492002-05-23 16:29:37 +000068
Jakob Eriksson33151992002-04-29 17:14:50 +000069 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
Francois Gougetc9493492002-05-23 16:29:37 +000070
Jakob Eriksson43b31772002-05-29 16:58:14 +000071 ok( strlen( sillytext ) == bytes_read, "file read size error" );
Jakob Eriksson33151992002-04-29 17:14:50 +000072
73 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
74 {
Jakob Eriksson43b31772002-05-29 16:58:14 +000075 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
76 ok( _hread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
Jakob Eriksson33151992002-04-29 17:14:50 +000077 for (i = 0; i < bytes_wanted; i++)
78 {
Jakob Eriksson43b31772002-05-29 16:58:14 +000079 ok( buffer[i] == sillytext[i], "that's not what's written" );
Jakob Eriksson33151992002-04-29 17:14:50 +000080 }
81 }
82
Jakob Eriksson43b31772002-05-29 16:58:14 +000083 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +000084
Alexandre Julliard75de8fe2002-06-22 00:08:10 +000085 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +000086}
87
Jakob Erikssone8449e62002-05-09 20:29:15 +000088
Jakob Eriksson33151992002-04-29 17:14:50 +000089static void test__hwrite( void )
90{
91 HFILE filehandle;
92 char buffer[10000];
93 long bytes_read;
94 UINT bytes_written;
95 UINT blocks;
96 UINT i;
97 char *contents;
98 HLOCAL memory_object;
99 char checksum[1];
100
101 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000102 if (filehandle == HFILE_ERROR)
103 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000104 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000105 return;
106 }
Jakob Eriksson33151992002-04-29 17:14:50 +0000107
Jakob Eriksson43b31772002-05-29 16:58:14 +0000108 ok( HFILE_ERROR != _hwrite( filehandle, "", 0 ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000109
Jakob Eriksson43b31772002-05-29 16:58:14 +0000110 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000111
112 filehandle = _lopen( filename, OF_READ );
Francois Gougetc9493492002-05-23 16:29:37 +0000113
Jakob Eriksson33151992002-04-29 17:14:50 +0000114 bytes_read = _hread( filehandle, buffer, 1);
115
Jakob Eriksson43b31772002-05-29 16:58:14 +0000116 ok( 0 == bytes_read, "file read size error" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000117
Jakob Eriksson43b31772002-05-29 16:58:14 +0000118 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000119
120 filehandle = _lopen( filename, OF_READWRITE );
121
122 bytes_written = 0;
123 checksum[0] = '\0';
124 srand( (unsigned)time( NULL ) );
125 for (blocks = 0; blocks < 100; blocks++)
126 {
127 for (i = 0; i < sizeof( buffer ); i++)
128 {
129 buffer[i] = rand( );
130 checksum[0] = checksum[0] + buffer[i];
131 }
Jakob Eriksson43b31772002-05-29 16:58:14 +0000132 ok( HFILE_ERROR != _hwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000133 bytes_written = bytes_written + sizeof( buffer );
134 }
135
Jakob Eriksson43b31772002-05-29 16:58:14 +0000136 ok( HFILE_ERROR != _hwrite( filehandle, checksum, 1 ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000137 bytes_written++;
138
Jakob Eriksson43b31772002-05-29 16:58:14 +0000139 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000140
141 memory_object = LocalAlloc( LPTR, bytes_written );
142
143 ok( 0 != memory_object, "LocalAlloc fails. (Could be out of memory.)" );
144
145 contents = LocalLock( memory_object );
146
147 filehandle = _lopen( filename, OF_READ );
148
149 contents = LocalLock( memory_object );
150
Jakob Eriksson43b31772002-05-29 16:58:14 +0000151 ok( NULL != contents, "LocalLock whines" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000152
Jakob Eriksson43b31772002-05-29 16:58:14 +0000153 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000154
155 checksum[0] = '\0';
156 i = 0;
157 do
158 {
159 checksum[0] = checksum[0] + contents[i];
160 i++;
161 }
162 while (i < bytes_written - 1);
163
Jakob Eriksson43b31772002-05-29 16:58:14 +0000164 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000165
Jakob Eriksson43b31772002-05-29 16:58:14 +0000166 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000167
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000168 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +0000169}
170
Jakob Erikssone8449e62002-05-09 20:29:15 +0000171
Jakob Eriksson33151992002-04-29 17:14:50 +0000172static void test__lclose( void )
173{
174 HFILE filehandle;
175
176 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000177 if (filehandle == HFILE_ERROR)
178 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000179 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000180 return;
181 }
Jakob Eriksson33151992002-04-29 17:14:50 +0000182
Jakob Eriksson43b31772002-05-29 16:58:14 +0000183 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000184
Jakob Eriksson43b31772002-05-29 16:58:14 +0000185 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000186
Jakob Eriksson43b31772002-05-29 16:58:14 +0000187 ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000188
Jakob Eriksson43b31772002-05-29 16:58:14 +0000189 ok( HFILE_ERROR == _lclose(filehandle), "_lclose should whine about this" );
Jakob Eriksson33151992002-04-29 17:14:50 +0000190
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000191 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Eriksson33151992002-04-29 17:14:50 +0000192}
193
194
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000195static void test__lcreat( void )
196{
197 HFILE filehandle;
198 char buffer[10000];
199 WIN32_FIND_DATAA search_results;
200
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
Jakob Eriksson43b31772002-05-29 16:58:14 +0000212 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( 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
Jakob Eriksson43b31772002-05-29 16:58:14 +0000242 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( 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
Jakob Eriksson43b31772002-05-29 16:58:14 +0000257 ok( _hread( filehandle, buffer, strlen( sillytext ) ) == strlen( 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( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000264}
265
266
267void test__llseek( void )
268{
269 INT i;
270 HFILE filehandle;
271 char buffer[1];
272 long bytes_read;
273
274 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000275 if (filehandle == HFILE_ERROR)
276 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000277 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000278 return;
279 }
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000280
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000281 for (i = 0; i < 400; i++)
282 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000283 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000284 }
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" );
287
288 bytes_read = _hread( filehandle, buffer, 1);
Jakob Eriksson43b31772002-05-29 16:58:14 +0000289 ok( 1 == bytes_read, "file read size error" );
290 ok( buffer[0] == sillytext[27], "_llseek error, it got lost seeking" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000291 ok( HFILE_ERROR != _llseek( filehandle, -400 * strlen( sillytext ), FILE_END ), "should be able to seek" );
292
293 bytes_read = _hread( filehandle, buffer, 1);
Jakob Eriksson43b31772002-05-29 16:58:14 +0000294 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" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000298
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000299 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000300}
301
Jakob Erikssone8449e62002-05-09 20:29:15 +0000302
303static void test__llopen( void )
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000304{
305 HFILE filehandle;
306 UINT bytes_read;
307 char buffer[10000];
308
309 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000310 if (filehandle == HFILE_ERROR)
311 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000312 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000313 return;
314 }
315
Jakob Eriksson43b31772002-05-29 16:58:14 +0000316 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
317 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000318
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 ) );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000322 ok( strlen( sillytext ) == bytes_read, "file read size error" );
323 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Francois Gougetc9493492002-05-23 16:29:37 +0000324
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000325 filehandle = _lopen( filename, OF_READWRITE );
326 bytes_read = _hread( filehandle, buffer, 2 * strlen( sillytext ) );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000327 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" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000330
331 filehandle = _lopen( filename, OF_WRITE );
Jakob Eriksson43b31772002-05-29 16:58:14 +0000332 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" );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000335
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000336 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000337 /* TODO - add tests for the SHARE modes - use two processes to pull this one off */
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000338}
339
Jakob Erikssone8449e62002-05-09 20:29:15 +0000340
341static void test__lread( void )
342{
343 HFILE filehandle;
344 char buffer[10000];
345 long bytes_read;
346 UINT bytes_wanted;
347 UINT i;
348
349 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000350 if (filehandle == HFILE_ERROR)
351 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000352 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000353 return;
354 }
Jakob Erikssone8449e62002-05-09 20:29:15 +0000355
Jakob Eriksson43b31772002-05-29 16:58:14 +0000356 ok( HFILE_ERROR != _hwrite( filehandle, sillytext, strlen( sillytext ) ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000357
Jakob Eriksson43b31772002-05-29 16:58:14 +0000358 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000359
360 filehandle = _lopen( filename, OF_READ );
361
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000362 ok( HFILE_ERROR != filehandle, "couldn't open file \"%s\" again (err=%ld)", filename, GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000363
Jakob Erikssone8449e62002-05-09 20:29:15 +0000364 bytes_read = _lread( filehandle, buffer, 2 * strlen( sillytext ) );
Francois Gougetc9493492002-05-23 16:29:37 +0000365
Jakob Eriksson43b31772002-05-29 16:58:14 +0000366 ok( strlen( sillytext ) == bytes_read, "file read size error" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000367
368 for (bytes_wanted = 0; bytes_wanted < strlen( sillytext ); bytes_wanted++)
369 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000370 ok( 0 == _llseek( filehandle, 0, FILE_BEGIN ), "_llseek complains" );
371 ok( _lread( filehandle, buffer, bytes_wanted ) == bytes_wanted, "erratic _hread return value" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000372 for (i = 0; i < bytes_wanted; i++)
373 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000374 ok( buffer[i] == sillytext[i], "that's not what's written" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000375 }
376 }
377
Jakob Eriksson43b31772002-05-29 16:58:14 +0000378 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000379
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000380 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000381}
382
383
384static void test__lwrite( void )
385{
386 HFILE filehandle;
387 char buffer[10000];
388 long bytes_read;
389 UINT bytes_written;
390 UINT blocks;
391 UINT i;
392 char *contents;
393 HLOCAL memory_object;
394 char checksum[1];
395
396 filehandle = _lcreat( filename, 0 );
Francois Gougetc9493492002-05-23 16:29:37 +0000397 if (filehandle == HFILE_ERROR)
398 {
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000399 ok(0,"couldn't create file \"%s\" (err=%ld)",filename,GetLastError());
Francois Gougetc9493492002-05-23 16:29:37 +0000400 return;
401 }
Jakob Erikssone8449e62002-05-09 20:29:15 +0000402
Jakob Eriksson43b31772002-05-29 16:58:14 +0000403 ok( HFILE_ERROR != _lwrite( filehandle, "", 0 ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000404
Jakob Eriksson43b31772002-05-29 16:58:14 +0000405 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000406
407 filehandle = _lopen( filename, OF_READ );
Francois Gougetc9493492002-05-23 16:29:37 +0000408
Jakob Erikssone8449e62002-05-09 20:29:15 +0000409 bytes_read = _hread( filehandle, buffer, 1);
410
Jakob Eriksson43b31772002-05-29 16:58:14 +0000411 ok( 0 == bytes_read, "file read size error" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000412
Jakob Eriksson43b31772002-05-29 16:58:14 +0000413 ok( HFILE_ERROR != _lclose(filehandle), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000414
415 filehandle = _lopen( filename, OF_READWRITE );
416
417 bytes_written = 0;
418 checksum[0] = '\0';
419 srand( (unsigned)time( NULL ) );
420 for (blocks = 0; blocks < 100; blocks++)
421 {
422 for (i = 0; i < sizeof( buffer ); i++)
423 {
424 buffer[i] = rand( );
425 checksum[0] = checksum[0] + buffer[i];
426 }
Jakob Eriksson43b31772002-05-29 16:58:14 +0000427 ok( HFILE_ERROR != _lwrite( filehandle, buffer, sizeof( buffer ) ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000428 bytes_written = bytes_written + sizeof( buffer );
429 }
430
Jakob Eriksson43b31772002-05-29 16:58:14 +0000431 ok( HFILE_ERROR != _lwrite( filehandle, checksum, 1 ), "_hwrite complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000432 bytes_written++;
433
Jakob Eriksson43b31772002-05-29 16:58:14 +0000434 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000435
436 memory_object = LocalAlloc( LPTR, bytes_written );
437
Jakob Eriksson43b31772002-05-29 16:58:14 +0000438 ok( 0 != memory_object, "LocalAlloc fails, could be out of memory" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000439
440 contents = LocalLock( memory_object );
441
442 filehandle = _lopen( filename, OF_READ );
443
444 contents = LocalLock( memory_object );
445
Jakob Eriksson43b31772002-05-29 16:58:14 +0000446 ok( NULL != contents, "LocalLock whines" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000447
Jakob Eriksson43b31772002-05-29 16:58:14 +0000448 ok( bytes_written == _hread( filehandle, contents, bytes_written), "read length differ from write length" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000449
450 checksum[0] = '\0';
451 i = 0;
452 do
453 {
Jakob Eriksson43b31772002-05-29 16:58:14 +0000454 checksum[0] += contents[i];
Jakob Erikssone8449e62002-05-09 20:29:15 +0000455 i++;
456 }
457 while (i < bytes_written - 1);
458
Jakob Eriksson43b31772002-05-29 16:58:14 +0000459 ok( checksum[0] == contents[i], "stored checksum differ from computed checksum" );
Alexandre Julliard8dc3a512002-05-10 01:10:04 +0000460
Jakob Eriksson43b31772002-05-29 16:58:14 +0000461 ok( HFILE_ERROR != _lclose( filehandle ), "_lclose complains" );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000462
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000463 ok( DeleteFileA( filename ) != 0, "DeleteFile failed (%ld)", GetLastError( ) );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000464}
465
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000466void test_CopyFileA(void)
467{
468 char temp_path[MAX_PATH];
469 char source[MAX_PATH], dest[MAX_PATH];
470 static const char prefix[] = "pfx";
471 DWORD ret;
472
473 ret = GetTempPathA(MAX_PATH, temp_path);
474 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
475 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
476
477 ret = GetTempFileNameA(temp_path, prefix, 0, source);
478 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
479
480 ret = GetTempFileNameA(temp_path, prefix, 0, dest);
481 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
482
483 ret = CopyFileA(source, dest, TRUE);
484 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
485 "CopyFileA: unexpected error %ld\n", GetLastError());
486
487 ret = CopyFileA(source, dest, FALSE);
488 ok(ret, "CopyFileA: error %ld\n", GetLastError());
489
490 ret = DeleteFileA(source);
491 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
492 ret = DeleteFileA(dest);
493 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
494}
495
496void test_CopyFileW(void)
497{
498 WCHAR temp_path[MAX_PATH];
499 WCHAR source[MAX_PATH], dest[MAX_PATH];
500 static const WCHAR prefix[] = {'p','f','x',0};
501 DWORD ret;
502
503 ret = GetTempPathW(MAX_PATH, temp_path);
504 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
505 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
506
507 ret = GetTempFileNameW(temp_path, prefix, 0, source);
508 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
509
510 ret = GetTempFileNameW(temp_path, prefix, 0, dest);
511 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
512
513 ret = CopyFileW(source, dest, TRUE);
514 ok(!ret && GetLastError() == ERROR_FILE_EXISTS,
515 "CopyFileW: unexpected error %ld\n", GetLastError());
516
517 ret = CopyFileW(source, dest, FALSE);
518 ok(ret, "CopyFileW: error %ld\n", GetLastError());
519
520 ret = DeleteFileW(source);
521 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
522 ret = DeleteFileW(dest);
523 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
524}
525
526void test_CreateFileA(void)
527{
528 HANDLE hFile;
529 char temp_path[MAX_PATH];
530 char filename[MAX_PATH];
531 static const char prefix[] = "pfx";
532 DWORD ret;
533
534 ret = GetTempPathA(MAX_PATH, temp_path);
535 ok(ret != 0, "GetTempPathA error %ld", GetLastError());
536 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
537
538 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
539 ok(ret != 0, "GetTempFileNameA error %ld", GetLastError());
540
541 hFile = CreateFileA(filename, GENERIC_READ, 0, NULL,
542 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
543 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
544 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
545
546 ret = DeleteFileA(filename);
547 ok(ret, "DeleteFileA: error %ld\n", GetLastError());
548}
549
550void test_CreateFileW(void)
551{
552 HANDLE hFile;
553 WCHAR temp_path[MAX_PATH];
554 WCHAR filename[MAX_PATH];
555 static const WCHAR prefix[] = {'p','f','x',0};
556 DWORD ret;
557
558 ret = GetTempPathW(MAX_PATH, temp_path);
559 ok(ret != 0, "GetTempPathW error %ld", GetLastError());
560 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH");
561
562 ret = GetTempFileNameW(temp_path, prefix, 0, filename);
563 ok(ret != 0, "GetTempFileNameW error %ld", GetLastError());
564
565 hFile = CreateFileW(filename, GENERIC_READ, 0, NULL,
566 CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
567 ok(hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS,
568 "CREATE_NEW should fail if file exists and last error value should be ERROR_FILE_EXISTS");
569
570 ret = DeleteFileW(filename);
571 ok(ret, "DeleteFileW: error %ld\n", GetLastError());
572}
573
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000574static void test_DeleteFileA( void )
575{
576 BOOL ret;
577 ret = DeleteFileA("");
578 ok((!ret) && (GetLastError() == ERROR_FILE_NOT_FOUND),
579 "DeleteFile should fail with an empty path, and last error value should be ERROR_FILE_NOT_FOUND");
580}
581
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000582#define PATTERN_OFFSET 0x10
583
584void test_offset_in_overlapped_structure(void)
585{
586 HANDLE hFile;
587 OVERLAPPED ov;
588 DWORD done;
589 BYTE buf[256], pattern[] = "TeSt";
590 UINT i;
591 char temp_path[MAX_PATH], temp_fname[MAX_PATH];
592
593 ok(GetTempPathA(MAX_PATH, temp_path) != 0, "GetTempPathA error %ld", GetLastError());
594 ok(GetTempFileNameA(temp_path, "pfx", 0, temp_fname) != 0, "GetTempFileNameA error %ld", GetLastError());
595
596 /*** Write File *****************************************************/
597
598 hFile = CreateFileA(temp_fname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
599 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
600
601 for(i = 0; i < sizeof(buf); i++) buf[i] = i;
602 ok(WriteFile(hFile, buf, sizeof(buf), &done, NULL), "WriteFile error %ld", GetLastError());
603 ok(done == sizeof(buf), "expected number of bytes written %lu", done);
604
605 memset(&ov, 0, sizeof(ov));
606 ov.Offset = PATTERN_OFFSET;
607 ov.OffsetHigh = 0;
608 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
609 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
610 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
611 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
612 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
613
614 ov.Offset = sizeof(buf) * 2;
615 ov.OffsetHigh = 0;
616 ok(WriteFile(hFile, pattern, sizeof(pattern), &done, &ov), "WriteFile error %ld", GetLastError());
617 ok(done == sizeof(pattern), "expected number of bytes written %lu", done);
618 /*trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));*/
619 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (sizeof(buf) * 2 + sizeof(pattern)),
620 "expected file offset %d", sizeof(buf) * 2 + sizeof(pattern));
621
622 CloseHandle(hFile);
623
624 /*** Read File *****************************************************/
625
626 hFile = CreateFileA(temp_fname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
627 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA error %ld", GetLastError());
628
629 memset(buf, 0, sizeof(buf));
630 memset(&ov, 0, sizeof(ov));
631 ov.Offset = PATTERN_OFFSET;
632 ov.OffsetHigh = 0;
633 ok(ReadFile(hFile, buf, sizeof(pattern), &done, &ov), "ReadFile error %ld", GetLastError());
634 ok(done == sizeof(pattern), "expected number of bytes read %lu", done);
635 trace("Current offset = %04lx\n", SetFilePointer(hFile, 0, NULL, FILE_CURRENT));
636 ok(SetFilePointer(hFile, 0, NULL, FILE_CURRENT) == (PATTERN_OFFSET + sizeof(pattern)),
637 "expected file offset %d", PATTERN_OFFSET + sizeof(pattern));
638 ok(!memcmp(buf, pattern, sizeof(pattern)), "pattern match failed");
639
640 CloseHandle(hFile);
641
642 ok(DeleteFileA(temp_fname), "DeleteFileA error %ld\n", GetLastError());
643}
Jakob Erikssone8449e62002-05-09 20:29:15 +0000644
Jakob Eriksson33151992002-04-29 17:14:50 +0000645START_TEST(file)
646{
647 test__hread( );
648 test__hwrite( );
649 test__lclose( );
Jakob Erikssonb02f0942002-05-06 20:12:55 +0000650 test__lcreat( );
651 test__llseek( );
652 test__llopen( );
Jakob Erikssone8449e62002-05-09 20:29:15 +0000653 test__lread( );
654 test__lwrite( );
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000655 test_CopyFileA();
656 test_CopyFileW();
657 test_CreateFileA();
658 test_CreateFileW();
Ryan Cummingc4aa0732002-11-12 02:11:52 +0000659 test_DeleteFileA();
Dmitry Timoshkov160ab192002-08-27 01:56:08 +0000660 test_offset_in_overlapped_structure();
Jakob Eriksson33151992002-04-29 17:14:50 +0000661}