[MSVCRT_WINETEST] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / rostests / winetests / msvcrt / file.c
index c253f88..2c356ea 100644 (file)
@@ -49,6 +49,8 @@ static HANDLE proc_handles[2];
 
 static int (__cdecl *p_fopen_s)(FILE**, const char*, const char*);
 static int (__cdecl *p__wfopen_s)(FILE**, const wchar_t*, const wchar_t*);
+static errno_t (__cdecl *p__get_fmode)(int*);
+static errno_t (__cdecl *p__set_fmode)(int);
 
 static const char* get_base_name(const char *path)
 {
@@ -71,6 +73,8 @@ static void init(void)
     p_fopen_s = (void*)GetProcAddress(hmod, "fopen_s");
     p__wfopen_s = (void*)GetProcAddress(hmod, "_wfopen_s");
     __pioinfo = (void*)GetProcAddress(hmod, "__pioinfo");
+    p__get_fmode = (void*)GetProcAddress(hmod, "_get_fmode");
+    p__set_fmode = (void*)GetProcAddress(hmod, "_set_fmode");
 }
 
 static void test_filbuf( void )
@@ -1789,6 +1793,16 @@ static void test_setmode(void)
         return;
     }
 
+    errno = 0xdeadbeef;
+    ret = _setmode(-2, 0);
+    ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
+    ok(errno == EINVAL, "errno = %d\n", errno);
+
+    errno = 0xdeadbeef;
+    ret = _setmode(-2, _O_TEXT);
+    ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
+    ok(errno == EBADF, "errno = %d\n", errno);
+
     fd = _open(name, _O_CREAT|_O_WRONLY, _S_IWRITE);
     ok(fd != -1, "failed to open file\n");
 
@@ -1850,6 +1864,11 @@ static void test_get_osfhandle(void)
 
     _close(fd);
     _unlink(fname);
+
+    errno = 0xdeadbeef;
+    handle = (HANDLE)_get_osfhandle(fd);
+    ok(handle == INVALID_HANDLE_VALUE, "_get_osfhandle returned %p\n", handle);
+    ok(errno == EBADF, "errno = %d\n", errno);
 }
 
 static void test_setmaxstdio(void)
@@ -2244,10 +2263,11 @@ static void test_write_flush_size(FILE *file, int bufsize)
     char *inbuffer;
     char *outbuffer;
     int size, fd;
+    fpos_t pos, pos2;
 
     fd = fileno(file);
-    inbuffer = calloc(bufsize + 1, 1);
-    outbuffer = calloc(bufsize + 1, 1);
+    inbuffer = calloc(1, bufsize + 1);
+    outbuffer = calloc(1, bufsize + 1);
     _snprintf(outbuffer, bufsize + 1, "0,1,2,3,4,5,6,7,8,9");
 
     for (size = bufsize + 1; size >= bufsize - 1; size--) {
@@ -2275,6 +2295,19 @@ static void test_write_flush_size(FILE *file, int bufsize)
     fseek(file, 0, SEEK_SET);
     ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
     ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize);
+
+    ok(!fseek(file, -1, SEEK_END), "fseek failed\n");
+    ok(!fgetpos(file, &pos), "fgetpos failed\n");
+    ok(fread(inbuffer, 1, 1, file) == 1, "fread failed\n");
+    ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
+    ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
+    ok(file->_ptr != file->_base, "file->_ptr == file->_base\n");
+    ok(fwrite(outbuffer, 1, bufsize, file), "fwrite failed\n");
+    ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
+    ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
+    ok(file->_ptr == file->_base, "file->_ptr == file->_base\n");
+    ok(!fgetpos(file, &pos2), "fgetpos failed\n");
+    ok(pos+bufsize+1 == pos2, "pos = %d (%d)\n", (int)pos, (int)pos2);
     free(inbuffer);
     free(outbuffer);
 }
@@ -2298,6 +2331,118 @@ static void test_write_flush(void)
     free(tempf);
 }
 
+static void test_close(void)
+{
+    ioinfo *stdout_info, stdout_copy, *stderr_info, stderr_copy;
+    int fd1, fd2, ret1, ret2, ret3, ret4;
+    DWORD flags;
+    HANDLE h;
+
+    /* test close on fds that use the same handle */
+    h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+    ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+    fd1 = _open_osfhandle((intptr_t)h, 0);
+    ok(fd1 != -1, "_open_osfhandle failed (%d)\n", errno);
+    fd2 = _open_osfhandle((intptr_t)h, 0);
+    ok(fd2 != -1, "_open_osfhandle failed (%d)\n", errno);
+    ok(fd1 != fd2, "fd1 == fd2\n");
+
+    ok((HANDLE)_get_osfhandle(fd1) == h, "handles mismatch (%p != %p)\n",
+            (HANDLE)_get_osfhandle(fd1), h);
+    ok((HANDLE)_get_osfhandle(fd2) == h, "handles mismatch (%p != %p)\n",
+            (HANDLE)_get_osfhandle(fd2), h);
+    ret1 = close(fd1);
+    ok(!ret1, "close(fd1) failed (%d)\n", errno);
+    ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n");
+    ok(close(fd2), "close(fd2) succeeded\n");
+
+    /* test close on stdout and stderr that use the same handle */
+    h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+    ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+    /* tests output will not be visible from now on */
+    stdout_info = &__pioinfo[STDOUT_FILENO/MSVCRT_FD_BLOCK_SIZE][STDOUT_FILENO%MSVCRT_FD_BLOCK_SIZE];
+    stderr_info = &__pioinfo[STDERR_FILENO/MSVCRT_FD_BLOCK_SIZE][STDERR_FILENO%MSVCRT_FD_BLOCK_SIZE];
+    stdout_copy = *stdout_info;
+    stderr_copy = *stderr_info;
+    stdout_info->handle = h;
+    stderr_info->handle = h;
+
+    ret1 = close(STDOUT_FILENO);
+    ret2 = GetHandleInformation(h, &flags);
+    ret3 = close(STDERR_FILENO);
+    ret4 = GetHandleInformation(h, &flags);
+
+    *stdout_info = stdout_copy;
+    *stderr_info = stderr_copy;
+    SetStdHandle(STD_OUTPUT_HANDLE, stdout_info->handle);
+    SetStdHandle(STD_ERROR_HANDLE, stderr_info->handle);
+    /* stdout and stderr restored */
+
+    ok(!ret1, "close(STDOUT_FILENO) failed\n");
+    ok(ret2, "GetHandleInformation failed\n");
+    ok(!ret3, "close(STDERR_FILENO) failed\n");
+    ok(!ret4, "GetHandleInformation succeeded\n");
+
+    DeleteFileA( "fdopen.tst" );
+}
+
+static void test__creat(void)
+{
+    int fd, pos, count, readonly, old_fmode = 0, have_fmode;
+    char buf[6], testdata[4] = {'a', '\n', 'b', '\n'};
+
+    have_fmode = p__get_fmode && p__set_fmode && !p__get_fmode(&old_fmode);
+    if (!have_fmode)
+        win_skip("_fmode can't be set, skipping mode tests\n");
+
+    if (have_fmode)
+        p__set_fmode(_O_TEXT);
+    fd = _creat("_creat.tst", 0);
+    ok(fd > 0, "_creat failed\n");
+    _write(fd, testdata, 4);
+    if (have_fmode) {
+        pos = _tell(fd);
+        ok(pos == 6, "expected pos 6 (text mode), got %d\n", pos);
+    }
+    ok(_lseek(fd, SEEK_SET, 0) == 0, "_lseek failed\n");
+    count = _read(fd, buf, 6);
+    ok(count == 4, "_read returned %d, expected 4\n", count);
+    count = count > 0 ? count > 4 ? 4 : count : 0;
+    ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
+    _close(fd);
+    readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
+    ok(readonly, "expected read-only file\n");
+    SetFileAttributesA("_creat.tst", FILE_ATTRIBUTE_NORMAL);
+    DeleteFileA("_creat.tst");
+
+    if (have_fmode)
+        p__set_fmode(_O_BINARY);
+    fd = _creat("_creat.tst", _S_IREAD | _S_IWRITE);
+    ok(fd > 0, "_creat failed\n");
+    _write(fd, testdata, 4);
+    if (have_fmode) {
+        pos = _tell(fd);
+        ok(pos == 4, "expected pos 4 (binary mode), got %d\n", pos);
+    }
+    ok(_lseek(fd, SEEK_SET, 0) == 0, "_lseek failed\n");
+    count = _read(fd, buf, 6);
+    ok(count == 4, "_read returned %d, expected 4\n", count);
+    count = count > 0 ? count > 4 ? 4 : count : 0;
+    ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
+    _close(fd);
+    readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
+    ok(!readonly, "expected rw file\n");
+    SetFileAttributesA("_creat.tst", FILE_ATTRIBUTE_NORMAL);
+    DeleteFileA("_creat.tst");
+
+    if (have_fmode)
+        p__set_fmode(old_fmode);
+}
+
 START_TEST(file)
 {
     int arg_c;
@@ -2364,6 +2509,8 @@ START_TEST(file)
     test_mktemp();
     test__open_osfhandle();
     test_write_flush();
+    test_close();
+    test__creat();
 
     /* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
      * file contains lines in the correct order