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 void test_asciifileops( void )
137 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9\r\n\r\nA,B,C,D,E\r\nX,Y,Z";
144 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
145 write (fd, outbuffer, sizeof (outbuffer));
148 /* Open file in ascii mode */
149 fd = open ("fdopen.tst", O_RDONLY);
150 file = fdopen (fd, "rb");
151 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
152 ok(ftell(file) == 0,"Did not start at beginning of file\n");
153 ok(fgets(buffer,sizeof(buffer),file) !=0,"line 1 fgets failed unexpected\n");
154 ok(fgets(buffer,sizeof(buffer),file) !=0,"line 2 fgets failed unexpected\n");
155 ok(fgets(buffer,sizeof(buffer),file) !=0,"line 3 fgets failed unexpected\n");
156 ok(fgets(buffer,sizeof(buffer),file) !=0,"line 4 fgets failed unexpected\n");
157 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
158 ok(feof(file) !=0,"feof doesn't signal EOF\n");
160 ok(ftell(file) == 0,"Did not rewind to beginning of file\n");
161 for (i=0; i<strlen(outbuffer); i++)
162 if (outbuffer[i] == '\n') break;
164 ok(fgets(buffer,strlen(outbuffer),file) !=0,"line 1 fgets failed unexpected\n");
166 todo_wine ok(l == i,"line 1 ftell got %ld should be %d\n", l, i);
167 ok(lstrlenA(buffer) == i-1,"line 1 fgets got size %d should be %d\n", lstrlenA(buffer), i-1);
168 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"line 2 fgets failed unexpected\n");
171 todo_wine ok(l == i,"line 2 ftell got %ld should be %d\n", l, i);
172 ok(lstrlenA(buffer) == 1,"line 2 fgets got size %d should be %d\n", lstrlenA(buffer), 1);
173 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"line 3 fgets failed unexpected\n");
174 for (; i<strlen(outbuffer); i++)
175 if (outbuffer[i] == '\n') break;
178 ok(l == i,"line 3 ftell got %ld should be %d\n", l, i);
182 unlink ("fdopen.tst");
186 static WCHAR* AtoW( char* p )
189 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
190 buffer = malloc( len * sizeof(WCHAR) );
191 MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
195 static void test_fgetwc( void )
201 static const char mytext[]= "This is test_fgetwc\n";
202 WCHAR wtextW[LLEN+1];
203 WCHAR *mytextW = NULL, *aptr, *wptr;
204 BOOL diff_found = FALSE;
207 tempf=_tempnam(".","wne");
208 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
209 fputs(mytext,tempfh);
211 tempfh = fopen(tempf,"rt");
212 fgetws(wtextW,LLEN,tempfh);
213 mytextW = AtoW ((char*)mytext);
217 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
219 diff_found |= (*aptr != *wptr);
221 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
222 if(mytextW) free (mytextW);
227 static void test_file_put_get( void )
231 static const char mytext[]= "This is a test_file_put_get\n";
232 static const char dostext[]= "This is a test_file_put_get\r\n";
234 WCHAR wtextW[LLEN+1];
235 WCHAR *mytextW = NULL, *aptr, *wptr;
236 BOOL diff_found = FALSE;
239 tempf=_tempnam(".","wne");
240 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
241 fputs(mytext,tempfh);
243 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
244 fgets(btext,LLEN,tempfh);
245 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
246 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
248 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
249 fputs(dostext,tempfh);
251 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
252 fgets(btext,LLEN,tempfh);
253 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
255 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
256 fgets(btext,LLEN,tempfh);
257 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
260 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
261 fgetws(wtextW,LLEN,tempfh);
262 mytextW = AtoW ((char*)mytext);
266 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
268 diff_found |= (*aptr != *wptr);
270 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
271 if(mytextW) free (mytextW);
276 static void test_file_write_read( void )
280 static const char mytext[]= "This is test_file_write_read\nsecond line\n";
281 static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
285 tempf=_tempnam(".","wne");
286 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
287 _S_IREAD | _S_IWRITE);
289 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
290 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
291 "_write _O_TEXT bad return value\n");
293 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
294 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
295 "_read _O_BINARY got bad length\n");
296 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
297 "problems with _O_TEXT _write / _O_BINARY _read\n");
298 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
300 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
301 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
302 "_read _O_TEXT got bad length\n");
303 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
304 "problems with _O_TEXT _write / _read\n");
307 memset(btext, 0, LLEN);
308 tempfd = _open(tempf,_O_APPEND|_O_RDWR); /* open for APPEND in default mode */
309 ok(tell(tempfd) == 0, "bad position %lu expecting 0\n", tell(tempfd));
310 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_APPEND got bad length\n");
311 ok( memcmp(mytext,btext,strlen(mytext)) == 0, "problems with _O_APPEND _read\n");
314 /* Test reading only \n or \r */
315 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
316 _lseek(tempfd, -1, FILE_END);
317 ret = _read(tempfd,btext,LLEN);
318 ok(ret == 1, "_read expected 1 got bad length: %d\n", ret);
319 _lseek(tempfd, -2, FILE_END);
320 ret = _read(tempfd,btext,LLEN);
321 ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
322 _lseek(tempfd, -3, FILE_END);
323 ret = _read(tempfd,btext,2);
324 todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
325 todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
329 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
331 tempf=_tempnam(".","wne");
332 tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0);
334 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
335 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
336 "_write _O_BINARY bad return value\n");
338 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
339 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
340 "_read _O_BINARY got bad length\n");
341 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
342 "problems with _O_BINARY _write / _read\n");
343 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
345 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
346 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
347 "_read _O_TEXT got bad length\n");
348 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
349 "problems with _O_BINARY _write / _O_TEXT _read\n");
352 ret =_chmod (tempf, _S_IREAD | _S_IWRITE);
354 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
356 ok( ret == 0 ,"Can't unlink '%s': %d\n", tempf, errno);
359 static void test_file_inherit_child(const char* fd_s)
365 ret =write(fd, "Success", 8);
366 ok( ret == 8, "Couldn't write in child process on %d (%s)\n", fd, strerror(errno));
367 lseek(fd, 0, SEEK_SET);
368 ok(read(fd, buffer, sizeof (buffer)) == 8, "Couldn't read back the data\n");
369 ok(memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
372 static void test_file_inherit_child_no(const char* fd_s)
377 ret = write(fd, "Success", 8);
378 ok( ret == -1 && errno == EBADF,
379 "Wrong write result in child process on %d (%s)\n", fd, strerror(errno));
382 static void test_file_inherit( const char* selfname )
385 const char* arg_v[5];
388 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY, _S_IREAD |_S_IWRITE);
389 ok(fd != -1, "Couldn't create test file\n");
391 arg_v[1] = "tests/file.c";
392 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
394 _spawnvp(_P_WAIT, selfname, arg_v);
395 ok(tell(fd) == 8, "bad position %lu expecting 8\n", tell(fd));
396 lseek(fd, 0, SEEK_SET);
397 ok(read(fd, buffer, sizeof (buffer)) == 8 && memcmp(buffer, "Success", 8) == 0, "Couldn't read back the data\n");
399 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
401 fd = open ("fdopen.tst", O_CREAT | O_RDWR | O_BINARY | O_NOINHERIT, _S_IREAD |_S_IWRITE);
402 ok(fd != -1, "Couldn't create test file\n");
404 arg_v[1] = "tests/file.c";
405 arg_v[2] = buffer; sprintf(buffer, "%d", fd);
408 _spawnvp(_P_WAIT, selfname, arg_v);
409 ok(tell(fd) == 0, "bad position %lu expecting 0\n", tell(fd));
410 ok(read(fd, buffer, sizeof (buffer)) == 0, "Found unexpected data (%s)\n", buffer);
412 ok(unlink("fdopen.tst") == 0, "Couldn't unlink\n");
415 static void test_tmpnam( void )
417 char name[MAX_PATH] = "abc";
421 ok(res != NULL, "tmpnam returned NULL\n");
422 ok(res[0] == '\\', "first character is not a backslash\n");
423 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
424 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
427 ok(res != NULL, "tmpnam returned NULL\n");
428 ok(res == name, "supplied buffer was not used\n");
429 ok(res[0] == '\\', "first character is not a backslash\n");
430 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
431 ok(res[strlen(res)-1] != '.', "second call - last character is a dot\n");
434 static void test_chsize( void )
437 long cur, pos, count;
438 char temptext[] = "012345678";
439 char *tempfile = _tempnam( ".", "tst" );
441 ok( tempfile != NULL, "Couldn't create test file: %s\n", tempfile );
443 fd = _open( tempfile, _O_CREAT|_O_TRUNC|_O_RDWR, _S_IREAD|_S_IWRITE );
444 ok( fd > 0, "Couldn't open test file\n" );
446 count = _write( fd, temptext, sizeof(temptext) );
447 ok( count > 0, "Couldn't write to test file\n" );
449 /* get current file pointer */
450 cur = _lseek( fd, 0, SEEK_CUR );
452 /* make the file smaller */
453 ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" );
455 pos = _lseek( fd, 0, SEEK_CUR );
456 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
457 ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" );
459 /* enlarge the file */
460 ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" );
462 pos = _lseek( fd, 0, SEEK_CUR );
463 ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos );
464 ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" );
470 static void test_fopen_fclose_fcloseall( void )
472 char fname1[] = "empty1";
473 char fname2[] = "empty2";
474 char fname3[] = "empty3";
475 FILE *stream1, *stream2, *stream3, *stream4;
478 /* testing fopen() */
479 stream1 = fopen(fname1, "w+");
480 ok(stream1 != NULL, "The file '%s' was not opened\n", fname1);
481 stream2 = fopen(fname2, "w ");
482 ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 );
484 stream3 = fopen(fname3, "r");
485 ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 );
486 stream3 = fopen(fname3, "w+");
487 ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 );
489 stream4 = fopen("", "w+");
490 ok(stream4 == NULL && errno == ENOENT,
491 "filename is empty, errno = %d (expected 2)\n", errno);
493 stream4 = fopen(NULL, "w+");
494 ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT),
495 "filename is NULL, errno = %d (expected 2 or 22)\n", errno);
497 /* testing fclose() */
498 ret = fclose(stream2);
499 ok(ret == 0, "The file '%s' was not closed\n", fname2);
500 ret = fclose(stream3);
501 ok(ret == 0, "The file '%s' was not closed\n", fname3);
502 ret = fclose(stream2);
503 ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret);
504 ret = fclose(stream3);
505 ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret);
507 /* testing fcloseall() */
508 numclosed = _fcloseall();
509 /* fname1 should be closed here */
510 ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed);
511 numclosed = _fcloseall();
512 ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed);
514 ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1);
515 ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2);
516 ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
519 static void test_get_osfhandle(void)
522 char fname[] = "t_get_osfhanle";
526 fd = _sopen(fname, _O_CREAT|_O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE);
527 handle = (HANDLE)_get_osfhandle(fd);
528 WriteFile(handle, "bar", 3, &bytes_written, NULL);
530 fd = _open(fname, _O_RDONLY, 0);
531 ok(fd != -1, "Coudn't open '%s' after _get_osfhanle()\n", fname);
542 arg_c = winetest_get_mainargs( &arg_v );
544 /* testing low-level I/O */
547 if (arg_c == 3) test_file_inherit_child(arg_v[2]);
548 else test_file_inherit_child_no(arg_v[2]);
551 test_file_inherit(arg_v[0]);
552 test_file_write_read();
555 /* testing stream I/O */
557 test_fopen_fclose_fcloseall();
563 test_get_osfhandle();