/* * Unit test suite for file functions * * Copyright 2002 Bill Currie * Copyright 2005 Paul Rupe * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #include "wine/test.h" #include #include #include #include #include #include #include #include #include #include #include #include #include static HANDLE proc_handles[2]; static void test_fdopen( void ) { static const char buffer[] = {0,1,2,3,4,5,6,7,8,9}; char ibuf[10]; int fd; FILE *file; fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE); write (fd, buffer, sizeof (buffer)); close (fd); fd = open ("fdopen.tst", O_RDONLY | O_BINARY); lseek (fd, 5, SEEK_SET); file = fdopen (fd, "rb"); ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n"); ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n"); fclose (file); unlink ("fdopen.tst"); } static void test_fileops( void ) { static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9"; char buffer[256]; WCHAR wbuffer[256]; int fd; FILE *file; fpos_t pos; int i, c; fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE); write (fd, outbuffer, sizeof (outbuffer)); close (fd); fd = open ("fdopen.tst", O_RDONLY | O_BINARY); file = fdopen (fd, "rb"); ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n"); ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n"); ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n"); ok(feof(file) !=0,"feof doesn't signal EOF\n"); rewind(file); ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n"); ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n"); ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n"); ok(strlen(buffer) == 1,"fgets dropped chars\n"); ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n"); rewind(file); for (i = 0, c = EOF; i < sizeof(outbuffer); i++) { ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n"); } ok((c = fgetc(file)) == EOF, "getc did not return EOF\n"); ok(feof(file), "feof did not return EOF\n"); ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n"); ok(feof(file), "feof after ungetc(EOF) did not return EOF\n"); ok((c = fgetc(file)) == EOF, "getc did not return EOF\n"); c = outbuffer[sizeof(outbuffer) - 1]; ok(ungetc(c, file) == c, "ungetc did not return its input\n"); ok(!feof(file), "feof after ungetc returned EOF\n"); ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n"); ok(c == outbuffer[sizeof(outbuffer) - 1], "getc did not return ungetc'd data\n"); ok(!feof(file), "feof after getc returned EOF prematurely\n"); ok((c = fgetc(file)) == EOF, "getc did not return EOF\n"); ok(feof(file), "feof after getc did not return EOF\n"); rewind(file); ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n"); ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos); pos = (ULONGLONG)sizeof (outbuffer); ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n"); ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n"); ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos); fclose (file); fd = open ("fdopen.tst", O_RDONLY | O_TEXT); file = fdopen (fd, "rt"); /* open in TEXT mode */ ok(fgetws(wbuffer,sizeof(wbuffer)/sizeof(wbuffer[0]),file) !=0,"fgetws failed unexpected\n"); ok(fgetws(wbuffer,sizeof(wbuffer)/sizeof(wbuffer[0]),file) ==0,"fgetws didn't signal EOF\n"); ok(feof(file) !=0,"feof doesn't signal EOF\n"); rewind(file); ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n"); ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n"); ok(fgetws(wbuffer,sizeof(outbuffer)/sizeof(outbuffer[0]),file) !=0,"fgets failed unexpected\n"); ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n"); fclose (file); file = fopen("fdopen.tst", "rb"); ok( file != NULL, "fopen failed\n"); /* sizeof(buffer) > content of file */ ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n"); /* feof should be set now */ ok(feof(file), "feof after fread failed\n"); fclose (file); unlink ("fdopen.tst"); } #define IOMODE (ao?"ascii mode":"binary mode") static void test_readmode( BOOL ascii_mode ) { 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"; static const char padbuffer[] = "ghjghjghjghj"; static const char nlbuffer[] = "\r\n"; char buffer[2*BUFSIZ+256]; const char *optr; int fd; FILE *file; const int *ip; int i, j, m, ao, pl; unsigned int fp; long l; fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE); /* an internal buffer of BUFSIZ is maintained, so make a file big * enough to test operations that cross the buffer boundary */ j = (2*BUFSIZ-4)/strlen(padbuffer); for (i=0; i 0, "Couldn't open test file\n" ); count = _write( fd, temptext, sizeof(temptext) ); ok( count > 0, "Couldn't write to test file\n" ); /* get current file pointer */ cur = _lseek( fd, 0, SEEK_CUR ); /* make the file smaller */ ok( _chsize( fd, sizeof(temptext) / 2 ) == 0, "_chsize() failed\n" ); pos = _lseek( fd, 0, SEEK_CUR ); ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos ); ok( _filelength( fd ) == sizeof(temptext) / 2, "Wrong file size\n" ); /* enlarge the file */ ok( _chsize( fd, sizeof(temptext) * 2 ) == 0, "_chsize() failed\n" ); pos = _lseek( fd, 0, SEEK_CUR ); ok( cur == pos, "File pointer changed from: %ld to: %ld\n", cur, pos ); ok( _filelength( fd ) == sizeof(temptext) * 2, "Wrong file size\n" ); _close( fd ); _unlink( tempfile ); } static void test_fopen_fclose_fcloseall( void ) { char fname1[] = "empty1"; char fname2[] = "empty2"; char fname3[] = "empty3"; FILE *stream1, *stream2, *stream3, *stream4; int ret, numclosed; /* testing fopen() */ stream1 = fopen(fname1, "w+"); ok(stream1 != NULL, "The file '%s' was not opened\n", fname1); stream2 = fopen(fname2, "w "); ok(stream2 != NULL, "The file '%s' was not opened\n", fname2 ); _unlink(fname3); stream3 = fopen(fname3, "r"); ok(stream3 == NULL, "The file '%s' shouldn't exist before\n", fname3 ); stream3 = fopen(fname3, "w+"); ok(stream3 != NULL, "The file '%s' should be opened now\n", fname3 ); errno = 0xfaceabad; stream4 = fopen("", "w+"); ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT), "filename is empty, errno = %d (expected 2 or 22)\n", errno); errno = 0xfaceabad; stream4 = fopen(NULL, "w+"); ok(stream4 == NULL && (errno == EINVAL || errno == ENOENT), "filename is NULL, errno = %d (expected 2 or 22)\n", errno); /* testing fclose() */ ret = fclose(stream2); ok(ret == 0, "The file '%s' was not closed\n", fname2); ret = fclose(stream3); ok(ret == 0, "The file '%s' was not closed\n", fname3); ret = fclose(stream2); ok(ret == EOF, "Closing file '%s' returned %d\n", fname2, ret); ret = fclose(stream3); ok(ret == EOF, "Closing file '%s' returned %d\n", fname3, ret); /* testing fcloseall() */ numclosed = _fcloseall(); /* fname1 should be closed here */ ok(numclosed == 1, "Number of files closed by fcloseall(): %u\n", numclosed); numclosed = _fcloseall(); ok(numclosed == 0, "Number of files closed by fcloseall(): %u\n", numclosed); ok(_unlink(fname1) == 0, "Couldn't unlink file named '%s'\n", fname1); ok(_unlink(fname2) == 0, "Couldn't unlink file named '%s'\n", fname2); ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3); } static void test_get_osfhandle(void) { int fd; char fname[] = "t_get_osfhanle"; DWORD bytes_written; HANDLE handle; fd = _sopen(fname, _O_CREAT|_O_RDWR, _SH_DENYRW, _S_IREAD | _S_IWRITE); handle = (HANDLE)_get_osfhandle(fd); WriteFile(handle, "bar", 3, &bytes_written, NULL); _close(fd); fd = _open(fname, _O_RDONLY, 0); ok(fd != -1, "Coudn't open '%s' after _get_osfhanle()\n", fname); _close(fd); _unlink(fname); } static void test_setmaxstdio(void) { ok(2048 == _setmaxstdio(2048),"_setmaxstdio returned %d instead of 2048\n",_setmaxstdio(2048)); ok(-1 == _setmaxstdio(2049),"_setmaxstdio returned %d instead of -1\n",_setmaxstdio(2049)); } static void test_stat(void) { int fd; int pipes[2]; struct stat buf; /* Tests for a file */ fd = open("stat.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE); if (fd >= 0) { ok(fstat(fd, &buf) == 0, "fstat failed: errno=%d", errno); ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode); ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode); ok(buf.st_dev == 0, "st_dev is %d, expected 0\n", buf.st_dev); ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev); ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size); ok(stat("stat.tst", &buf) == 0, "stat failed: errno=%d", errno); ok((buf.st_mode & _S_IFMT) == _S_IFREG, "bad format = %06o\n", buf.st_mode); ok((buf.st_mode & 0777) == 0666, "bad st_mode = %06o\n", buf.st_mode); ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev); ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size); close(fd); remove("stat.tst"); } else skip("open failed with errno %d\n", errno); /* Tests for a char device */ if (_dup2(0, 10) == 0) { ok(fstat(10, &buf) == 0, "fstat(stdin) failed: errno=%d\n", errno); if ((buf.st_mode & _S_IFMT) == _S_IFCHR) { ok(buf.st_mode == _S_IFCHR, "bad st_mode=%06o\n", buf.st_mode); ok(buf.st_dev == 10, "st_dev is %d, expected 10\n", buf.st_dev); ok(buf.st_rdev == 10, "st_rdev is %d, expected 10\n", buf.st_rdev); ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); } else skip("stdin is not a char device? st_mode=%06o\n", buf.st_mode); close(10); } else skip("_dup2 failed with errno %d\n", errno); /* Tests for pipes */ if (_pipe(pipes, 1024, O_BINARY) == 0) { ok(fstat(pipes[0], &buf) == 0, "fstat(pipe) failed: errno=%d\n", errno); ok(buf.st_mode == _S_IFIFO, "bad st_mode=%06o\n", buf.st_mode); ok(buf.st_dev == pipes[0], "st_dev is %d, expected %d\n", buf.st_dev, pipes[0]); ok(buf.st_rdev == pipes[0], "st_rdev is %d, expected %d\n", buf.st_rdev, pipes[0]); ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); close(pipes[0]); close(pipes[1]); } else skip("pipe failed with errno %d\n", errno); } static const char* pipe_string="Hello world"; /* How many messages to transfer over the pipe */ #define N_TEST_MESSAGES 3 static void test_pipes_child(int argc, char** args) { int fd; int nwritten; int i; if (argc < 5) { ok(0, "not enough parameters: %d\n", argc); return; } fd=atoi(args[3]); ok(close(fd) == 0, "unable to close %d: %d\n", fd, errno); fd=atoi(args[4]); for (i=0; i 0) buf[r]='\0'; ok(strcmp(buf, pipe_string) == 0, "expected to read '%s', got '%s'\n", pipe_string, buf); } r=read(pipes[0], buf, sizeof(buf)-1); ok(r == 0, "expected to read 0 bytes, got %d\n", r); ok(close(pipes[0]) == 0, "unable to close %d: %d\n", pipes[0], errno); /* Test reading from a pipe with fread() */ if (_pipe(pipes, 1024, O_BINARY) < 0) { ok(0, "pipe failed with errno %d\n", errno); return; } arg_v[0] = selfname; arg_v[1] = "tests/file.c"; arg_v[2] = "pipes"; arg_v[3] = str_fdr; sprintf(str_fdr, "%d", pipes[0]); arg_v[4] = str_fdw; sprintf(str_fdw, "%d", pipes[1]); arg_v[5] = NULL; proc_handles[1] = (HANDLE)_spawnvp(_P_NOWAIT, selfname, arg_v); ok(close(pipes[1]) == 0, "unable to close %d: %d\n", pipes[1], errno); file=fdopen(pipes[0], "r"); /* In blocking mode, fread will keep calling read() until it gets * enough bytes, or EOF, even on Unix. (If this were a Unix terminal * in cooked mode instead of a pipe, it would also stop on EOL.) */ expected[0] = 0; for (i=0; i 0) buf[r]='\0'; ok(strcmp(buf, expected) == 0, "got '%s' expected '%s'\n", buf, expected); /* Let child close the file before we read, so we can sense EOF reliably */ Sleep(100); r=fread(buf, 1, sizeof(buf)-1, file); ok(r == 0, "fread() returned %d instead of 0\n", r); ok(ferror(file) == 0, "got ferror() = %d\n", ferror(file)); ok(feof(file), "feof() is false!\n"); ok(fclose(file) == 0, "unable to close the pipe: %d\n", errno); } static void test_unlink(void) { FILE* file; ok(mkdir("test_unlink") == 0, "unable to create test dir\n"); file = fopen("test_unlink\\empty", "w"); ok(file != NULL, "unable to create test file\n"); if(file) fclose(file); ok(_unlink("test_unlink") != 0, "unlinking a non-empty directory must fail\n"); unlink("test_unlink\\empty"); rmdir("test_unlink"); } START_TEST(file) { int arg_c; char** arg_v; arg_c = winetest_get_mainargs( &arg_v ); /* testing low-level I/O */ if (arg_c >= 3) { if (strcmp(arg_v[2], "inherit") == 0) test_file_inherit_child(arg_v[3]); else if (strcmp(arg_v[2], "inherit_no") == 0) test_file_inherit_child_no(arg_v[3]); else if (strcmp(arg_v[2], "pipes") == 0) test_pipes_child(arg_c, arg_v); else ok(0, "invalid argument '%s'\n", arg_v[2]); return; } test_file_inherit(arg_v[0]); test_file_write_read(); test_chsize(); test_stat(); test_unlink(); /* testing stream I/O */ test_fdopen(); test_fopen_fclose_fcloseall(); test_fileops(); test_asciimode(); test_readmode(FALSE); /* binary mode */ test_readmode(TRUE); /* ascii mode */ test_fgetc(); test_fputc(); test_flsbuf(); test_fgetwc(); test_ctrlz(); test_file_put_get(); test_tmpnam(); test_get_osfhandle(); test_setmaxstdio(); test_pipes(arg_v[0]); /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report * file contains lines in the correct order */ WaitForMultipleObjects(sizeof(proc_handles)/sizeof(proc_handles[0]), proc_handles, TRUE, 5000); }