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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/test.h"
35 static void test_fdopen( void )
37 static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
42 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
43 write (fd, buffer, sizeof (buffer));
46 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
47 lseek (fd, 5, SEEK_SET);
48 file = fdopen (fd, "rb");
49 ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
50 ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
52 unlink ("fdopen.tst");
55 static void test_fileops( void )
57 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
65 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
66 write (fd, outbuffer, sizeof (outbuffer));
69 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
70 file = fdopen (fd, "rb");
71 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
72 ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
73 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
74 ok(feof(file) !=0,"feof doesn't signal EOF\n");
76 ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
77 ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
78 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
79 ok(strlen(buffer) == 1,"fgets dropped chars\n");
80 ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
83 for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
85 ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
87 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
88 ok(feof(file), "feof did not return EOF\n");
89 ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n");
90 ok(feof(file), "feof after ungetc(EOF) did not return EOF\n");
91 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
92 c = outbuffer[sizeof(outbuffer) - 1];
93 ok(ungetc(c, file) == c, "ungetc did not return its input\n");
94 ok(!feof(file), "feof after ungetc returned EOF\n");
95 ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n");
96 ok(c == outbuffer[sizeof(outbuffer) - 1],
97 "getc did not return ungetc'd data\n");
98 ok(!feof(file), "feof after getc returned EOF prematurely\n");
99 ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
100 ok(feof(file), "feof after getc did not return EOF\n");
103 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
104 ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
105 pos = (ULONGLONG)sizeof (outbuffer);
106 ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
107 ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
108 ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
111 fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
112 file = fdopen (fd, "rt"); /* open in TEXT mode */
113 ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
114 ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
115 ok(feof(file) !=0,"feof doesn't signal EOF\n");
117 ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
118 ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
119 ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
120 ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
122 unlink ("fdopen.tst");
125 static WCHAR* AtoW( char* p )
128 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
129 buffer = malloc( len * sizeof(WCHAR) );
130 MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
134 static void test_fgetwc( void )
140 static const char mytext[]= "This is test_fgetwc\n";
141 WCHAR wtextW[LLEN+1];
142 WCHAR *mytextW = NULL, *aptr, *wptr;
143 BOOL diff_found = FALSE;
146 tempf=_tempnam(".","wne");
147 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
148 fputs(mytext,tempfh);
150 tempfh = fopen(tempf,"rt");
151 fgetws(wtextW,LLEN,tempfh);
152 mytextW = AtoW ((char*)mytext);
156 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
158 diff_found |= (*aptr != *wptr);
160 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
161 if(mytextW) free (mytextW);
166 static void test_file_put_get( void )
170 static const char mytext[]= "This is a test_file_put_get\n";
171 static const char dostext[]= "This is a test_file_put_get\r\n";
173 WCHAR wtextW[LLEN+1];
174 WCHAR *mytextW = NULL, *aptr, *wptr;
175 BOOL diff_found = FALSE;
178 tempf=_tempnam(".","wne");
179 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
180 fputs(mytext,tempfh);
182 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
183 fgets(btext,LLEN,tempfh);
184 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
185 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
187 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
188 fputs(dostext,tempfh);
190 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
191 fgets(btext,LLEN,tempfh);
192 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
194 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
195 fgets(btext,LLEN,tempfh);
196 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
199 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
200 fgetws(wtextW,LLEN,tempfh);
201 mytextW = AtoW ((char*)mytext);
205 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
207 diff_found |= (*aptr != *wptr);
209 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
210 if(mytextW) free (mytextW);
215 static void test_file_write_read( void )
219 static const char mytext[]= "This is test_file_write_read\nsecond line\n";
220 static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
224 tempf=_tempnam(".","wne");
225 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
226 _S_IREAD | _S_IWRITE);
228 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
229 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
230 "_write _O_TEXT bad return value\n");
232 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
233 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
234 "_read _O_BINARY got bad length\n");
235 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
236 "problems with _O_TEXT _write / _O_BINARY _read\n");
237 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
239 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
240 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
241 "_read _O_TEXT got bad length\n");
242 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
243 "problems with _O_TEXT _write / _read\n");
246 memset(btext, 0, LLEN);
247 tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
248 ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
249 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
250 ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
253 /* Test reading only \n or \r */
254 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
255 _lseek(tempfd, -1, FILE_END);
256 ret = _read(tempfd,btext,LLEN);
257 ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
258 _lseek(tempfd, -2, FILE_END);
259 ret = _read(tempfd,btext,LLEN);
260 ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
261 _lseek(tempfd, -3, FILE_END);
262 ret = _read(tempfd,btext,2);
263 todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
264 todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
268 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
270 tempf=_tempnam(".","wne");
271 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
273 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
274 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
275 "_write _O_BINARY bad return value\n");
277 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
278 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
279 "_read _O_BINARY got bad length\n");
280 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
281 "problems with _O_BINARY _write / _read\n");
282 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
284 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
285 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
286 "_read _O_TEXT got bad length\n");
287 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
288 "problems with _O_BINARY _write / _O_TEXT _read\n");
291 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
293 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
295 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
298 static void test_file_inherit_child(const char* fd_s)
304 ret =write(fd, "Success", 8);
305 ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
306 lseek(fd, 0, SEEK_SET);
307 ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
308 ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
311 static void test_file_inherit_child_no(const char* fd_s)
316 ret = write(fd, "Success", 8);
317 ok( ret == -1 && errno == EBADF,
318 "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
321 static void test_file_inherit( const char* selfname )
324 const char* arg_v[5];
327 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
328 ok(fd != -1, "Couldn't create test file\n");
330 arg_v[1] = "tests/file.c";
331 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
333 _spawnvp(_P_WAIT, selfname, arg_v);
334 ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
335 lseek(fd, 0, SEEK_SET);
336 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
338 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
340 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
341 ok(fd != -1, "Couldn't create test file\n");
343 arg_v[1] = "tests/file.c";
344 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
347 _spawnvp(_P_WAIT, selfname, arg_v);
348 ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
349 ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
351 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
354 static void test_tmpnam( void )
356 char name[MAX_PATH] = "abc";
360 ok(res != NULL, "tmpnam returned NULL\n");
361 ok(res[0] == '\\', "first character is not a backslash\n");
362 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
363 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
366 ok(res != NULL, "tmpnam returned NULL\n");
367 ok(res == name, "supplied buffer was not used\n");
368 ok(res[0] == '\\', "first character is not a backslash\n");
369 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
370 ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
373 static void test_chsize( void )
376 long cur, pos, count;
377 char temptext[] = "012345678";
378 char *tempfile = _tempnam( ".", "tst" );
380 ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
382 fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
383 ok( fd > 0, "Couldn't open test file\n" );
385 count = _write( fd, temptext, sizeof(temptext) );
386 ok( count > 0, "Couldn't write to test file\n" );
388 /* get current file pointer */
389 cur = _lseek( fd, 0, SEEK_CUR );
391 /* make the file smaller */
392 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
394 pos = _lseek( fd, 0, SEEK_CUR );
395 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
396 ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
398 /* enlarge the file */
399 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
401 pos = _lseek( fd, 0, SEEK_CUR );
402 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
403 ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
409 static void test_fopen_fclose_fcloseall( void )
411 char fname1[] = "empty1";
412 char fname2[] = "empty2";
413 char fname3[] = "empty3";
414 FILE *stream1, *stream2, *stream3, *stream4;
417 /* testing fopen() */
418 stream1 = fopen(fname1, "w+");
419 ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
420 stream2 = fopen(fname2, "w ");
421 ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
423 stream3 = fopen(fname3, "r");
424 ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
425 stream3 = fopen(fname3, "w+");
426 ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
428 stream4 = fopen("", "w+");
429 ok(stream4 == NULL && errno == ENOENT,
430 "filename is empty, errno = %d (expected 2)\n", errno);
432 stream4 = fopen(NULL, "w+");
433 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
434 "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
436 /* testing fclose() */
437 ret = fclose(stream2);
438 ok(ret == 0, "The file '%s' was not closed\n", fname2);
439 ret = fclose(stream3);
440 ok(ret == 0, "The file '%s' was not closed\n", fname3);
441 ret = fclose(stream2);
442 ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
443 ret = fclose(stream3);
444 ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
446 /* testing fcloseall() */
447 numclosed = _fcloseall();
448 /* fname1 should be closed here */
449 ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
450 numclosed = _fcloseall();
451 ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
453 ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
454 ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
455 ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
463 arg_c = winetest_get_mainargs( &arg_v );
465 /* testing low-level I/O */
468 if (arg_c == 3) test_file_inherit_child(arg_v[2]);
469 else test_file_inherit_child_no(arg_v[2]);
472 test_file_inherit(arg_v[0]);
473 test_file_write_read();
476 /* testing stream I/O */
478 test_fopen_fclose_fcloseall();