2 * Unit test suite for file functions
4 * Copyright 2002 Bill Currie
5 * Copyright 2005 Paul Rupe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/test.h"
36 static void test_fdopen( void )
38 static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
43 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
44 write (fd, buffer, sizeof (buffer));
47 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
48 lseek (fd, 5, SEEK_SET);
49 file = fdopen (fd, "rb");
50 ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
51 ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
53 unlink ("fdopen.tst");
56 static void test_fileops( void )
58 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
66 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
67 write (fd, outbuffer, sizeof (outbuffer));
70 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
71 file = fdopen (fd, "rb");
72 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
73 ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
74 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
75 ok(feof(file) !=0,"feof doesn't signal EOF\n");
77 ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
78 ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
79 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
80 ok(strlen(buffer) == 1,"fgets dropped chars\n");
81 ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
84 for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
86 ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
88 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
89 ok(feof(file), "feof did not return EOF\n");
90 ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n");
91 ok(feof(file), "feof after ungetc(EOF) did not return EOF\n");
92 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
93 c = outbuffer[sizeof(outbuffer) - 1];
94 ok(ungetc(c, file) == c, "ungetc did not return its input\n");
95 ok(!feof(file), "feof after ungetc returned EOF\n");
96 ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n");
97 ok(c == outbuffer[sizeof(outbuffer) - 1],
98 "getc did not return ungetc'd data\n");
99 ok(!feof(file), "feof after getc returned EOF prematurely\n");
100 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
101 ok(feof(file), "feof after getc did not return EOF\n");
104 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
105 ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
106 pos = (ULONGLONG)sizeof (outbuffer);
107 ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
108 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
109 ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
112 fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
113 file = fdopen (fd, "rt"); /* open in TEXT mode */
114 ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
115 ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
116 ok(feof(file) !=0,"feof doesn't signal EOF\n");
118 ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
119 ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
120 ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
121 ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
124 file = fopen("fdopen.tst", "rb");
125 ok( file != NULL, "fopen failed\n");
126 /* sizeof(buffer) > content of file */
127 ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
128 /* feof should be set now */
129 ok(feof(file), "feof after fread failed\n");
132 unlink ("fdopen.tst");
135 static WCHAR* AtoW( char* p )
138 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
139 buffer = malloc( len * sizeof(WCHAR) );
140 MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
144 static void test_fgetwc( void )
150 static const char mytext[]= "This is test_fgetwc\n";
151 WCHAR wtextW[LLEN+1];
152 WCHAR *mytextW = NULL, *aptr, *wptr;
153 BOOL diff_found = FALSE;
156 tempf=_tempnam(".","wne");
157 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
158 fputs(mytext,tempfh);
160 tempfh = fopen(tempf,"rt");
161 fgetws(wtextW,LLEN,tempfh);
162 mytextW = AtoW ((char*)mytext);
166 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
168 diff_found |= (*aptr != *wptr);
170 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
171 if(mytextW) free (mytextW);
176 static void test_file_put_get( void )
180 static const char mytext[]= "This is a test_file_put_get\n";
181 static const char dostext[]= "This is a test_file_put_get\r\n";
183 WCHAR wtextW[LLEN+1];
184 WCHAR *mytextW = NULL, *aptr, *wptr;
185 BOOL diff_found = FALSE;
188 tempf=_tempnam(".","wne");
189 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
190 fputs(mytext,tempfh);
192 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
193 fgets(btext,LLEN,tempfh);
194 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
195 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
197 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
198 fputs(dostext,tempfh);
200 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
201 fgets(btext,LLEN,tempfh);
202 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
204 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
205 fgets(btext,LLEN,tempfh);
206 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
209 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
210 fgetws(wtextW,LLEN,tempfh);
211 mytextW = AtoW ((char*)mytext);
215 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
217 diff_found |= (*aptr != *wptr);
219 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
220 if(mytextW) free (mytextW);
225 static void test_file_write_read( void )
229 static const char mytext[]= "This is test_file_write_read\nsecond line\n";
230 static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
234 tempf=_tempnam(".","wne");
235 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
236 _S_IREAD | _S_IWRITE);
238 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
239 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
240 "_write _O_TEXT bad return value\n");
242 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
243 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
244 "_read _O_BINARY got bad length\n");
245 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
246 "problems with _O_TEXT _write / _O_BINARY _read\n");
247 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
249 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
250 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
251 "_read _O_TEXT got bad length\n");
252 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
253 "problems with _O_TEXT _write / _read\n");
256 memset(btext, 0, LLEN);
257 tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
258 ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
259 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
260 ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
263 /* Test reading only \n or \r */
264 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
265 _lseek(tempfd, -1, FILE_END);
266 ret = _read(tempfd,btext,LLEN);
267 ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
268 _lseek(tempfd, -2, FILE_END);
269 ret = _read(tempfd,btext,LLEN);
270 ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
271 _lseek(tempfd, -3, FILE_END);
272 ret = _read(tempfd,btext,2);
273 todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
274 todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
278 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
280 tempf=_tempnam(".","wne");
281 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
283 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
284 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
285 "_write _O_BINARY bad return value\n");
287 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
288 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
289 "_read _O_BINARY got bad length\n");
290 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
291 "problems with _O_BINARY _write / _read\n");
292 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
294 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
295 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
296 "_read _O_TEXT got bad length\n");
297 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
298 "problems with _O_BINARY _write / _O_TEXT _read\n");
301 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
303 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
305 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
308 static void test_file_inherit_child(const char* fd_s)
314 ret =write(fd, "Success", 8);
315 ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
316 lseek(fd, 0, SEEK_SET);
317 ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
318 ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
321 static void test_file_inherit_child_no(const char* fd_s)
326 ret = write(fd, "Success", 8);
327 ok( ret == -1 && errno == EBADF,
328 "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
331 static void test_file_inherit( const char* selfname )
334 const char* arg_v[5];
337 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
338 ok(fd != -1, "Couldn't create test file\n");
340 arg_v[1] = "tests/file.c";
341 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
343 _spawnvp(_P_WAIT, selfname, arg_v);
344 ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
345 lseek(fd, 0, SEEK_SET);
346 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
348 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
350 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
351 ok(fd != -1, "Couldn't create test file\n");
353 arg_v[1] = "tests/file.c";
354 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
357 _spawnvp(_P_WAIT, selfname, arg_v);
358 ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
359 ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
361 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
364 static void test_tmpnam( void )
366 char name[MAX_PATH] = "abc";
370 ok(res != NULL, "tmpnam returned NULL\n");
371 ok(res[0] == '\\', "first character is not a backslash\n");
372 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
373 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
376 ok(res != NULL, "tmpnam returned NULL\n");
377 ok(res == name, "supplied buffer was not used\n");
378 ok(res[0] == '\\', "first character is not a backslash\n");
379 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
380 ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
383 static void test_chsize( void )
386 long cur, pos, count;
387 char temptext[] = "012345678";
388 char *tempfile = _tempnam( ".", "tst" );
390 ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
392 fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
393 ok( fd > 0, "Couldn't open test file\n" );
395 count = _write( fd, temptext, sizeof(temptext) );
396 ok( count > 0, "Couldn't write to test file\n" );
398 /* get current file pointer */
399 cur = _lseek( fd, 0, SEEK_CUR );
401 /* make the file smaller */
402 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
404 pos = _lseek( fd, 0, SEEK_CUR );
405 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
406 ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
408 /* enlarge the file */
409 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
411 pos = _lseek( fd, 0, SEEK_CUR );
412 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
413 ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
419 static void test_fopen_fclose_fcloseall( void )
421 char fname1[] = "empty1";
422 char fname2[] = "empty2";
423 char fname3[] = "empty3";
424 FILE *stream1, *stream2, *stream3, *stream4;
427 /* testing fopen() */
428 stream1 = fopen(fname1, "w+");
429 ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
430 stream2 = fopen(fname2, "w ");
431 ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
433 stream3 = fopen(fname3, "r");
434 ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
435 stream3 = fopen(fname3, "w+");
436 ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
438 stream4 = fopen("", "w+");
439 ok(stream4 == NULL && errno == ENOENT,
440 "filename is empty, errno = %d (expected 2)\n", errno);
442 stream4 = fopen(NULL, "w+");
443 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
444 "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
446 /* testing fclose() */
447 ret = fclose(stream2);
448 ok(ret == 0, "The file '%s' was not closed\n", fname2);
449 ret = fclose(stream3);
450 ok(ret == 0, "The file '%s' was not closed\n", fname3);
451 ret = fclose(stream2);
452 ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
453 ret = fclose(stream3);
454 ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
456 /* testing fcloseall() */
457 numclosed = _fcloseall();
458 /* fname1 should be closed here */
459 ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
460 numclosed = _fcloseall();
461 ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
463 ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
464 ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
465 ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
468 static void test_get_osfhandle(void)
471 char fname[] = "t_get_osfhanle";
475 fd = _sopen(fname, _O_CREAT|_O_RDWR, _SH_DENYRW, 0);
476 handle = (HANDLE)_get_osfhandle(fd);
477 WriteFile(handle, "bar", 3, &bytes_written, NULL);
479 fd = _open(fname, _O_RDONLY, 0);
480 ok(fd != -1, "Coudn't open '%s' after _get_osfhanle()\n", fname);
491 arg_c = winetest_get_mainargs( &arg_v );
493 /* testing low-level I/O */
496 if (arg_c == 3) test_file_inherit_child(arg_v[2]);
497 else test_file_inherit_child_no(arg_v[2]);
500 test_file_inherit(arg_v[0]);
501 test_file_write_read();
504 /* testing stream I/O */
506 test_fopen_fclose_fcloseall();
511 test_get_osfhandle();