Chromium Code Reviews| 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..535d3c622fa58fc2756317fe5fbbe2aef89fb723 100644 |
| --- a/base/files/file_util_unittest.cc |
| +++ b/base/files/file_util_unittest.cc |
| @@ -7,6 +7,7 @@ |
| #include <algorithm> |
| #include <fstream> |
| +#include <initializer_list> |
| #include <set> |
| #include <utility> |
| #include <vector> |
| @@ -246,6 +247,30 @@ std::wstring ReadTextFile(const FilePath& filename) { |
| return std::wstring(contents); |
| } |
| +// Sets |is_inheritable| to indicate whether or not |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). Calls to this function must be wrapped with |
| +// ASSERT_NO_FATAL_FAILURE to properly abort tests in case of fatal failure. |
| +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. |
| @@ -1711,6 +1736,24 @@ TEST_F(FileUtilTest, GetTempDirTest) { |
| } |
| #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"}) { |
| + SCOPED_TRACE(mode); |
| + ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_path, L"Geepers")); |
| + FILE* file = OpenFile(file_path, mode); |
| + ASSERT_NE(nullptr, file); |
| + bool is_inheritable = true; |
| + ASSERT_NO_FATAL_FAILURE(GetIsInheritable(file, &is_inheritable)); |
|
Mark Mentovai
2017/02/11 00:52:29
You do want to reach CloseFile() even if this fail
grt (UTC plus 2)
2017/02/13 07:53:28
Good point. Moved CloseFile into a ScopedClosureRu
|
| + 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++) { |