Index: base/files/file_util_unittest.cc |
diff --git a/base/files/file_util_unittest.cc b/base/files/file_util_unittest.cc |
index 7ef60a4b29881d0016adf5564956f5d1c749bd0a..1792daffc640ead97fa27e1cb066df006584fe62 100644 |
--- a/base/files/file_util_unittest.cc |
+++ b/base/files/file_util_unittest.cc |
@@ -2,11 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <fcntl.h> |
#include <stddef.h> |
#include <stdint.h> |
#include <algorithm> |
#include <fstream> |
+#include <initializer_list> |
#include <set> |
#include <utility> |
#include <vector> |
@@ -246,6 +248,29 @@ std::wstring ReadTextFile(const FilePath& filename) { |
return std::wstring(contents); |
} |
+// Sets |is_inheritable| to true if |stream| is set up to be inerhited into |
+// child processes (i.e., HANDLE_FLAG_INHERIT is set on the underlying handle on |
+// Windows, or FD_CLOEXEC is not set on the underlying file descriptor on |
+// POSIX). |
+void GetIsInheritable(FILE* stream, bool* is_inheritable) { |
+#if defined(OS_WIN) |
+ HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))); |
+ ASSERT_NE(INVALID_HANDLE_VALUE, handle); |
+ |
+ DWORD info = 0; |
+ ASSERT_EQ(TRUE, ::GetHandleInformation(handle, &info)); |
+ *is_inheritable = ((info & HANDLE_FLAG_INHERIT) != 0); |
+#elif defined(OS_POSIX) |
+ int fd = fileno(stream); |
+ ASSERT_NE(-1, fd); |
+ int flags = fcntl(fd, F_GETFD, 0); |
+ ASSERT_NE(-1, flags); |
+ *is_inheritable = ((flags & FD_CLOEXEC) == 0); |
+#else |
+ FAIL() << "Unimplemented"; |
+#endif |
+} |
+ |
TEST_F(FileUtilTest, FileAndDirectorySize) { |
// Create three files of 20, 30 and 3 chars (utf8). ComputeDirectorySize |
// should return 53 bytes. |
@@ -1709,8 +1734,26 @@ TEST_F(FileUtilTest, GetTempDirTest) { |
::_tputenv_s(kTmpKey, _T("")); |
} |
} |
+ |
#endif // OS_WIN |
+// Test that files opened by OpenFile are not set up for inheritance into child |
+// procs. |
+TEST_F(FileUtilTest, OpenFileNoInheritance) { |
+ FilePath file_path(temp_dir_.GetPath().Append(FPL("a_file"))); |
+ |
+ for (const char* mode : {"wb", "r,ccs=UNICODE"}) { |
+ ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers")); |
+ FILE* file = OpenFile(file_path, mode); |
+ ASSERT_NE(nullptr, file); |
+ bool is_inheritable = false; |
+ ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); |
+ EXPECT_FALSE(is_inheritable); |
+ ASSERT_TRUE(CloseFile(file)); |
+ ASSERT_TRUE(DeleteFile(file_path, false)); |
+ } |
+} |
+ |
TEST_F(FileUtilTest, CreateTemporaryFileTest) { |
FilePath temp_files[3]; |
for (int i = 0; i < 3; i++) { |